Java实现将txt文件转成xls文件的方法
作者:Frank_lyn 时间:2022-05-20 10:21:25
最近项目用到txt文件和xls文件的转换,这里记录一下具体的思路。
下面利用java代码实现txt转xls,这里要使用到jxl.jar包,这个包是通过java来操作Excel表格的工具类库。
该jar包支持字体、数字、日期操作,能够修饰单元格属性,还能够支持图像和图表,基本上已经满足我们的日常操作,最主要的是这套API是纯Java实现的,在Windows和Linux操作系统下,它都可以正确的处理Excel文件。
具体实现代码如下:
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class txtToxls {
//txt文本路径
static String txtFilePath = "D:\\Super_PLU.txt";
//xls路径
static String xlsFilePath = "D:\\Super_PLU.xls";
//每一列的列名
static String c1Name, c2Name, c3Name, c4Name, c5Name, c6Name, c7Name, c8Name;
public static void main(String args[]) {
// 将txt文件进行解析,保存为List
ArrayList<TxtFile> xlsList = getTxtInfos();
// 将List以xls保存
TransToExcel(xlsList);
}
private static ArrayList<TxtFile> getTxtInfos() {
ArrayList<TxtFile> txtFileList = new ArrayList<TxtFile>();
BufferedReader bufferedReader = null;
try {
// 这里注意指定文件的编码格式
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(txtFilePath), "gbk"));
String element = null;
int index = 0;
while ((element = bufferedReader.readLine()) != null) {
//如果是此行为空,则跳过
if(element.trim().equals("")){
continue;
}
//第一行作为每列名称
String[] value = element.trim().split(",");
if (index == 0) {
c1Name = value[0];
c2Name = value[1];
c3Name = value[2];
c4Name = value[3];
c5Name = value[4];
c6Name = value[5];
c7Name = value[6];
c8Name = value[7];
index = 1;
continue;
}
//从第二行开始读取每行内容,以TxtFile形式存储
TxtFile txtFile = new TxtFile(Integer.parseInt(value[0]), Integer.parseInt(value[1]), value[2], value[3], value[4], Integer.parseInt(value[5]), Integer.parseInt(value[6]), Integer.parseInt(value[7]));
txtFileList.add(txtFile);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return txtFileList;
}
private static void TransToExcel(ArrayList<TxtFile> txtFileList) {
WritableWorkbook book = null;
try {
// 创建一个xls文件
book = Workbook.createWorkbook(new File(xlsFilePath));
// 生成名为'商品信息'的工作表,这里参数0表示第一页
WritableSheet sheet = book.createSheet("商品信息", 0);
// 在Label对象为每一列添加列名,即每一列的第一行
Label label1 = new Label(0, 0, c1Name);
Label label2 = new Label(1, 0, c2Name);
Label label3 = new Label(2, 0, c3Name);
Label label4 = new Label(3, 0, c4Name);
Label label5 = new Label(4, 0, c5Name);
Label label6 = new Label(5, 0, c6Name);
Label label7 = new Label(6, 0, c7Name);
Label label8 = new Label(7, 0, c8Name);
// 将定义好列名添加到工作表中
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
sheet.addCell(label5);
sheet.addCell(label6);
sheet.addCell(label7);
sheet.addCell(label8);
/*
* 遍历传进来的List,把每一行的内容再顺序加入到工作表中,
* 在生成数字单元格时, 必须使用Number的完整包路径
*/
for (int i = 0; i < txtFileList.size(); i++) {
TxtFile p = txtFileList.get(i);
jxl.write.Number item_code = new jxl.write.Number(0, (i+1), p.item_code);
jxl.write.Number plu = new jxl.write.Number(1, (i+1), p.plu);
Label commodity = new Label(2, (i+1), p.commodity);
Label ingredient= new Label(3, (i+1), p.ingredient);
Label special = new Label(4, (i+1), p.special);
jxl.write.Number use_by_date = new jxl.write.Number(5, (i+1), p.use_by_date);
jxl.write.Number use_by_date_print = new jxl.write.Number(6, (i+1), p.use_by_date_print);
jxl.write.Number packge_by_date_print = new jxl.write.Number(7, (i+1), p.packge_by_date_print);
sheet.addCell(item_code);
sheet.addCell(plu);
sheet.addCell(commodity);
sheet.addCell(ingredient);
sheet.addCell(special);
sheet.addCell(use_by_date);
sheet.addCell(use_by_date_print);
sheet.addCell(packge_by_date_print);
}
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();;
}
}
}
// txt文件model类
class TxtFile {
int item_code;
int plu;
String commodity;
String ingredient;
String special;
int use_by_date;
int use_by_date_print;
int packge_by_date_print;
public TxtFile(int item_code, int plu, String commodity, String ingredient, String special,int use_by_date, int use_by_date_print, int packge_by_date_print) {
this.item_code = item_code;
this.plu = plu;
this.commodity = commodity;
this.ingredient = ingredient;
this.special = special;
this.use_by_date = use_by_date;
this.use_by_date_print = use_by_date_print;
this.packge_by_date_print = packge_by_date_print;
}
}
来源:https://blog.csdn.net/omelon1/article/details/78783851
标签:Java,txt,xls
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一起来学习Java IO的转化流
2023-11-26 20:38:30
![](https://img.aspxhome.com/file/2023/2/72612_0s.png)
Java数据结构之顺序表的实现
2023-06-22 00:47:26
![](https://img.aspxhome.com/file/2023/8/57418_0s.jpg)
使用java代码获取新浪微博应用的access token代码实例
2023-12-01 20:18:30
Java中泛型的示例详解
2023-10-27 02:54:24
基于SpringBoot+Redis的Session共享与单点登录详解
2023-09-22 18:13:48
windows下java环境变量的设置方法
2022-12-01 03:13:14
Spring的@Autowired加到接口上但获取的是实现类的问题
2023-08-23 21:32:21
Apache SkyWalking 修复TTL timer 失效bug详解
2023-08-18 08:09:53
![](https://img.aspxhome.com/file/2023/5/58155_0s.png)
C# 泛型List排序的实现
2021-09-22 07:03:16
![](https://img.aspxhome.com/file/2023/8/82158_0s.png)
详解SpringBoot配置文件启动时动态配置参数方法
2022-03-05 03:20:02
![](https://img.aspxhome.com/file/2023/0/80280_0s.jpg)
java文件操作工具类分享(file文件工具类)
2023-11-24 22:32:47
Java C++ 算法题解leetcode145商品折扣后最终价格单调栈
2023-09-16 23:29:42
![](https://img.aspxhome.com/file/2023/3/61123_0s.png)
spring-boot读取props和yml配置文件的方法
2022-06-01 14:54:26
SpringBoot中的HATEOAS详情
2022-12-27 03:12:31
Spring JPA联表查询之注解属性详解
2021-11-04 14:19:04
![](https://img.aspxhome.com/file/2023/9/67599_0s.jpg)
详解SpringBoot缓存的实例代码(EhCache 2.x 篇)
2023-04-05 04:55:18
![](https://img.aspxhome.com/file/2023/1/77931_0s.jpg)
maven中profile的使用
2022-03-31 10:43:53
基于Java解决华为机试之字符串加解密
2022-10-12 17:32:39
解析Java中如何获取Spring中配置的bean
2023-07-20 13:35:26
详解WPF中的APP生命周期以及全局异常捕获
2022-12-10 11:54:27