本段代码的目的是从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);
}
}
}

最新文章

  1. WTFPL 开源协议
  2. 区别 PHP 的 $_POST、$HTTP_RAW_POST_DATA 和 php://input
  3. MVC 依赖注入扩展
  4. 使用ProxychainsMac下安装及配置
  5. svn安装
  6. SQL Server 2005无法远程连接的解决方法
  7. 数字时钟DigClock
  8. Redis 安装与简单示例
  9. ubuntu-使用终端配置网络
  10. git 查看某个文件的历史修改版本
  11. 1611: [Usaco2008 Feb]Meteor Shower流星雨
  12. Reportng配置报告地址
  13. Heartbeat实现集群高可用热备
  14. NET快速信息化系统开发框架 V3.2 -&gt; “用户管理”主界面使用多表头展示、增加打印功能
  15. python爬虫之正则表达式
  16. Spring ConfigurationClassPostProcessor Bean解析及自注册过程
  17. Php实现版本比较接口
  18. 新服务器上装java PHP环境有什么一键安装的方便的方法?一般都是怎么安装环境的?
  19. 2.23 js处理日历控件(修改readonly属性)
  20. OC语言-runtime

热门文章

  1. jQuery遍历Table表格的行和列
  2. Android 应用开机自启和无需权限开启悬浮框
  3. mysql服务器系统优化
  4. linux 命令——45 free(转)
  5. 【洛谷1580】yyy loves Easter_Egg I(字符串处理题)
  6. 【luogu P1637 三元上升子序列】 题解
  7. shell脚本,awk结合正则来打印文件里面的内容。
  8. servlet从服务器磁盘文件读出到浏览器显示,中文乱码问题,不要忘记在输入流和输出流都要设置编码格式,否则一个地方没设置不统一就会各种乱码
  9. PAT 乙级 1003
  10. 【图论】[USACO]控制公司 Controlling Companies