本段代码的目的是从txt文本中读取相应格式的数据,然后写入到对应格式的excel文档中

在敲本段代码的时候,也学习了一些其它知识点,如下:

1、byte[] b_charset= String.getBytes("charset");就是返回字符串在给定的charset编码格式下的表现,保存在byte[]数组里面。charset可以是utf-8,gbk,iso8859-1等

2、String s_charset = new String(b_charset,"charset");使用指定编码格式把byte[]解析成String格式。charset可以是utf-8,gbk,iso8859-1等

3、System.exit(0)是正常退出程序,而System.exit(1)或者说非0表示非正常退出程序

第1第2条学习来源:String的getBytes()方法

第3条学习来源:System.exit(0)和System.exit(1)区别

读取txt生成excel学习来源:Java生成和操作Excel文件

完整代码如下:

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException; import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException; public class txt2excel {
public static void main(String[] args) { File file = new File("C:\\Users\\Desktop\\bbb.txt");// 将读取的txt文件
File file2 = new File("C:\\Users\\Desktop\\work.xls");// 将生成的excel表格 if (file.exists() && file.isFile()) { InputStreamReader read = null;
String line = "";
BufferedReader input = null;
WritableWorkbook wbook = null;
WritableSheet sheet; try {
read = new InputStreamReader(new FileInputStream(file), "gbk");
input = new BufferedReader(read); wbook = Workbook.createWorkbook(file2);// 根据路径生成excel文件
sheet = wbook.createSheet("first", 0);// 新标签页 try {
Label company = new Label(0, 0, "公司名称");// 如下皆为列名
sheet.addCell(company);
Label position = new Label(1, 0, "岗位");
sheet.addCell(position);
Label salary = new Label(2, 0, "薪资");
sheet.addCell(salary);
Label status = new Label(3, 0, "状态");
sheet.addCell(status);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} int m = 1;// excel行数
int n = 0;// excel列数
Label t;
while ((line = input.readLine()) != null) { String[] words = line.split("[ \t]");// 把读出来的这行根据空格或tab分割开 for (int i = 0; i < words.length; i++) {
if (!words[i].matches("\\s*")) { // 当不是空行时
t = new Label(n, m, words[i].trim());
sheet.addCell(t);
n++;
}
}
n = 0;// 回到列头部
m++;// 向下移动一行
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} finally {
try {
wbook.write();
wbook.close();
input.close();
read.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
System.out.println("over!");
System.exit(0);
} else {
System.out.println("file is not exists or not a file");
System.exit(0);
}
}
}

Java-读取txt生成excel的更多相关文章

  1. Java 读取txt文件生成Word文档

    本文将以Java程序代码为例介绍如何读取txt文件中的内容,生成Word文档.在编辑代码前,可参考如下代码环境进行配置: IntelliJ IDEA Free Spire.Doc for Java T ...

  2. Java读取txt文件、excel文件的方法

    Java读取txt文件.excel文件的方法 1.读取txt文件 public static String getFileContent(String filePath,String charset) ...

  3. Java读取txt文件

    package com.loongtao.general.crawler.slave.utils; import java.io.BufferedReader; import java.io.File ...

  4. java 读取TXT文件的方法

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  5. java读取TXT文件的方法

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  6. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  7. Java读取txt文件信息并操作。

    一.java读取txt文件内容 import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.Fi ...

  8. java读取txt

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  9. java读取txt文件内容

    package read; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public ...

随机推荐

  1. TCP的连接和释放过程

    TCP的连接和释放过程 1.三次握手的过程 1)主机A向主机B发送TCP连接请求数据包,其中包含主机A的初始序列号seq(A)=x.(其中报文中同步标志位SYN=1,ACK=0,表示这是一个TCP连接 ...

  2. spring的工厂方法

    http://blog.csdn.net/nvd11/article/details/51542360

  3. struts2的执行流程

    在浏览器端输入相应的访问地址>>>>把请求发送给tomact>>>>tomact判断应该交给那个webApplication>>>&g ...

  4. iOS编程规范(整理)

    一.文档结构管理 1.建立Libraries文件夹,所有第三方库放入其中. 2.建立Utilities文件夹,自已封装的类放入其中. 3.建立Constants.h头文件,所有的常量定义于其中.Con ...

  5. 为Visual Studio 2012添加MSDN离线帮助

    之前有网络的情况下,一直使用的都是在线的,最近又有笔记本上面有时使用时没有网络,所以就想使用下离线的MSDN包.可是找了半天,发现都是需要再次进行下载的.VS2012使用的帮助程序是HelpViewe ...

  6. mac上Apache修改目录浏览权限

    sudo vim /etc/apache2/httpd.conf <Directory "/Library/WebServer/Documents"> # # Poss ...

  7. Hive之数据模型

    (本文是基于多篇文章根据个人理解进行的整合,参考的文章见末尾的整理) 数据模型 hive的数据模型包括:database.table.partition和bucket. 1.Database:相当于关 ...

  8. systemd 中的requires, wants, before, after

    man systemd.unit    man systemd.service ###依赖关系和前后顺序* 依赖关系:Requires和Wants * 前后顺序:After,Before 依赖关系,前 ...

  9. pymysql 简单操作数据库

    #!/usr/bin/env python #-*- coding:utf-8 -*- # author:leo # datetime:2019/4/24 15:22 # software: PyCh ...

  10. js数据结构处理--------树结构数据遍历

    1.深度遍历 深度遍历利用栈来实现 class Stack { constructor () { this.top = 0, // 栈的长度 this.list = [] } push(item) { ...