Java实现将txt文件转成xls文件的方法
作者:Frank_lyn 发布时间:2022-05-20 10:21:25
标签:Java,txt,xls
最近项目用到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语言中的反射机制可以操作字节码文件,可以读和修改
- 最近一段时间 某台服务器上的一个应用总是隔一段时间就自己挂掉 用top看了看 从重新部署应用开始没有多长时间CPU占用上升得很快排查步骤1.
- 本文实例为大家分享了Java实现简单日历界面的具体代码,供大家参考,具体内容如下请使用JFrame、JPanel、JButton、JLabe
- 一、传递参数既可以通过值也可以通过引用传递参数。通过引用传递参数允许函数成员(方法、属性、索引器、运算符和构造函数)更改参数的值,并保持该更
- // Path类 IO命名空间 静态类 不能创建对象类名.string str =@"E:\C#程序设计基础入门教程\(第十一天)
- Web控件DropDownList和WinForm控件ComboBox机制不一样。ComboBox没有对应的ListItem需要自己写一个:
- ThymeleafThymeleaf是最近SpringBoot推荐支持的模板框架,官网在thymeleaf.org这里。我们为什么要用Thy
- 常用的Dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框AlertDialog的常用方法setT
- 首先我们要做的就是先把IIS(Internet信息服务)打开,我用的是win8 的系统,所以这里以win8系统的操作来讲一、IIS的一些事先
- 网络唤醒实现了对网络的集中管理,即在任何时刻,网管中心的IT管理人员可以经由网络远程唤醒一台处于休眠或关机状态的计算机。使用这一功能,IT管
- 一、医院接口本文继续开发分布式医疗挂号系统,进入到医院信息、科室、排版接口的开发,内容比较枯燥。关于医院医院信息的上传接口实现,已经在上一篇
- 简单几步,实现SpringMVC+servlet3.0文件上传功能:第一步:配置web.xml文件中的servlet,添加multipart
- 本文实例讲述了android编程实现图片库的封装方法。分享给大家供大家参考,具体如下:大家在做安卓应用的时候 经常要从网络中获取图片 都是通
- Android CardView详解Android5.0中向我们介绍了一个全新的控件–CardView,从本质上看,可以将Car
- 前言公司最近在开发中遇到一个问题,在弄帖子的发布与回复问题,然后再iOS端和Android端添加表情的时候都会出错Caused by: ja
- Android:控件GridView的使用如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView
- 与任何程序设计语言一样,Java使用条件语句和循环结构确定控制流。本文将简单讲解条件、循环和switch。一、块作用域块(block),即复
- 无论使用何种IDE开发Android,集成官方Android SDK并创建Android工程之后,该工程都会默认包括一整套Android项目
- 给对象按照字符串属性进行排序在java中对象进行排序,排序的属性是string,我们只需要实现Comparator接口,然后实现比较的方式。
- 简述最近做的公司项目,图片比较多,不想给其存储到自己服务器上,就买了阿里云的OSS服务器来哦进行存储,其实集成第三方平台,一般没什么难度,当