最近项目用到txt文件和xls文件的转换,这里记录一下具体的思路。
下面利用java代码实现txt转xls,这里要使用到jxl.jar包,这个包是通过java来操作excel表格的工具类库。
该jar包支持字体、数字、日期操作,能够修饰单元格属性,还能够支持图像和图表,基本上已经满足我们的日常操作,最主要的是这套api是纯java实现的,在windows和linux操作系统下,它都可以正确的处理excel文件。
具体实现代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
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;
}
}
|
以上这篇java实现将txt文件转成xls文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/omelon1/article/details/78783851