最近項目用到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