Java-读取txt生成excel
本段代码的目的是从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的更多相关文章
- Java 读取txt文件生成Word文档
本文将以Java程序代码为例介绍如何读取txt文件中的内容,生成Word文档.在编辑代码前,可参考如下代码环境进行配置: IntelliJ IDEA Free Spire.Doc for Java T ...
- Java读取txt文件、excel文件的方法
Java读取txt文件.excel文件的方法 1.读取txt文件 public static String getFileContent(String filePath,String charset) ...
- Java读取txt文件
package com.loongtao.general.crawler.slave.utils; import java.io.BufferedReader; import java.io.File ...
- java 读取TXT文件的方法
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- java读取TXT文件的方法
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- java操作office和pdf文件java读取word,excel和pdf文档内容
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
- Java读取txt文件信息并操作。
一.java读取txt文件内容 import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.Fi ...
- java读取txt
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- java读取txt文件内容
package read; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public ...
随机推荐
- JSP的使用
JSP全称为(Java server page),之所以出现JSP,最主要的目的是抽离出Servlet中输出HTML的部分,由于之前响应客户端的时候,是直接在Servlet中利用response.ge ...
- 美团Java面试154道题
Java集合22题 ArrayList 和 Vector 的区别.ArrayList与Vector区别 说说 ArrayList,Vector, LinkedList 的存储性能和特性.ArrayLi ...
- 前端JS电商放大镜效果
前端JS电商放大镜效果: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- python3爬虫03(find_all用法等)
#read1.html文件# <html><head><title>The Dormouse's story</title></head># ...
- SQL 语句 使用附加和分离
use mastergo declare @flg int --返回0表示成功 否则表示失败declare @msg varchar(50) --显示成功或失败的消息declare @dbname v ...
- Mysql数据库插入时乱码问题解决
我们在利用cmd的黑屏界面进行mysql数据的插入时往往会出现不能插入的情况,这个原因是因为我们系统虽然和服务器端即mysql的数据库采用的都是统一的utf8的编码,但是在传输的过程中会变成iso88 ...
- pta数据结构编程题
编程题6 树的同构 编程题7 List Leaves 编程题8 Tree Traversals Again 编程题10 Root of AVL Tree 编程题12 堆中的路径 编程题13 File ...
- Sql Server配置管理器与 Sql Server Management Studio
起初只安装了Sql Server配置管理器,之后用Navicat连接,总是报错(命名管道提供程序: 无法打开与 SQL Server 的连接 [53]) (另记:Navicat的“主机名或IP地址:” ...
- POJ 2718 Smallest Difference(dfs,剪枝)
枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...
- Android(java)学习笔记88:BaseAdapter适配器重写之getView()
1. BaseAdapter适配器重写 之getView(): (1)View getview(int position, View convertview, ViewGroup parent ) 第 ...