今天有個朋友問我,有沒有excel表格到處json的方法,在網上找到了好幾個工具,都不太理想,于是根據自己的需求,自己寫了一個工具。
功能代碼
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
package org.duang.test; import java.io.file; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import net.sf.json.jsonarray; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.dateutil; import org.apache.poi.ss.usermodel.formulaevaluator; import org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.usermodel.sheet; import org.apache.poi.ss.usermodel.workbook; import org.apache.poi.ss.usermodel.workbookfactory; /** * excel表格轉成json * @classname: excel2jsonhelper * @description:todo(這里用一句話描述這個類的作用) * @author liyonghui * @date 2017年1月6日 下午4:42:43 */ public class excel2jsonhelper { //常亮,用作第一種模板類型,如下圖 private static final int header_value_type_z= 1 ; //第二種模板類型,如下圖 private static final int header_value_type_s= 2 ; public static void main(string[] args) { file dir = new file( "e:\\2003.xls" ); excel2jsonhelper excelhelper = getexcel2jsonhelper(); //dir文件,0代表是第一行為保存到數據庫或者實體類的表頭,一般為英文的字符串,2代表是第二種模板, jsonarray jsonarray = excelhelper.readexcle(dir, 0 , 2 ); system.out.println(jsonarray.tostring());; } /** * * 獲取一個實例 */ private static excel2jsonhelper getexcel2jsonhelper(){ return new excel2jsonhelper(); } /** * 文件過濾 * @title: filenamefileter * @description: todo(這里用一句話描述這個方法的作用) * @param: * @author liyonghui * @date 2017年1月6日 下午4:45:42 * @return: void * @throws */ private boolean filenamefileter(file file){ boolean endswith = false ; if (file != null ){ string filename = file.getname(); endswith = filename.endswith( ".xls" ) || filename.endswith( ".xlsx" ); } return endswith; } /** * 獲取表頭行 * @title: getheaderrow * @description: todo(這里用一句話描述這個方法的作用) * @param: @param sheet * @param: @param index * @param: @return * @author liyonghui * @date 2017年1月6日 下午5:05:24 * @return: row * @throws */ private row getheaderrow(sheet sheet, int index){ row headerrow = null ; if (sheet!= null ){ headerrow = sheet.getrow(index); } return headerrow; } /** * 獲取表格中單元格的value * @title: getcellvalue * @description: todo(這里用一句話描述這個方法的作用) * @param: @param row * @param: @param cellindex * @param: @param formula * @param: @return * @author liyonghui * @date 2017年1月6日 下午5:40:28 * @return: object * @throws */ private object getcellvalue(row row, int cellindex,formulaevaluator formula){ cell cell = row.getcell(cellindex); if (cell != null ){ switch (cell.getcelltype()) { //string類型 case cell.cell_type_string: return cell.getrichstringcellvalue().getstring(); //number類型 case cell.cell_type_numeric: if (dateutil.iscelldateformatted(cell)) { return cell.getdatecellvalue().gettime(); } else { return cell.getnumericcellvalue(); } //boolean類型 case cell.cell_type_boolean: return cell.getbooleancellvalue(); //公式 case cell.cell_type_formula: return formula.evaluate(cell).getnumbervalue(); default : return null ; } } return null ; } /** * 獲取表頭value * @title: getheadercellvalue * @description: todo(這里用一句話描述這個方法的作用) * @param: @param headerrow * @param: @param cellindex 英文表頭所在的行,從0開始計算哦 * @param: @param type 表頭的類型第一種 姓名(name)英文于實體類或者數據庫中的變量一致 * @param: @return * @author liyonghui * @date 2017年1月6日 下午6:12:21 * @return: string * @throws */ private string getheadercellvalue(row headerrow, int cellindex, int type){ cell cell = headerrow.getcell(cellindex); string headervalue = null ; if (cell != null ){ //第一種模板類型 if (type == header_value_type_z){ headervalue = cell.getrichstringcellvalue().getstring(); int l_bracket = headervalue.indexof( "(" ); int r_bracket = headervalue.indexof( ")" ); if (l_bracket == - 1 ){ l_bracket = headervalue.indexof( "(" ); } if (r_bracket == - 1 ){ r_bracket = headervalue.indexof( ")" ); } headervalue = headervalue.substring(l_bracket+ 1 , r_bracket); } else if (type == header_value_type_s){ //第二種模板類型 headervalue = cell.getrichstringcellvalue().getstring(); } } return headervalue; } /** * 讀取excel表格 * @title: readexcle * @description: todo(這里用一句話描述這個方法的作用) * @param: @param file * @param: @param headerindex * @param: @param headtype 表頭的類型第一種 姓名(name)英文于實體類或者數據庫中的變量一致 * @author liyonghui * @date 2017年1月6日 下午6:13:27 * @return: void * @throws */ public jsonarray readexcle(file file, int headerindex, int headtype){ list<map<string, object>> lists = new arraylist<map<string, object>>(); if (!filenamefileter(file)){ return null ; } else { try { //加載excel表格 workbookfactory wbfactory = new workbookfactory(); workbook wb = wbfactory.create(file); //讀取第一個sheet頁 sheet sheet = wb.getsheetat( 0 ); //讀取表頭行 row headerrow = getheaderrow(sheet, headerindex); //讀取數據 formulaevaluator formula = wb.getcreationhelper().createformulaevaluator(); for ( int r = headerindex+ 1 ; r<= sheet.getlastrownum();r++){ row datarow = sheet.getrow(r); map<string, object> map = new hashmap<string, object>(); for ( int h = 0 ; h<datarow.getlastcellnum();h++){ //表頭為key string key = getheadercellvalue(headerrow,h,headtype); //數據為value object value = getcellvalue(datarow, h, formula); if (!key.equals( "" ) && !key.equals( "null" ) && key != null ){ map.put(key, value); } } lists.add(map); } } catch (exception e) { e.printstacktrace(); } } jsonarray jsonarray = jsonarray.fromobject(lists); return jsonarray; } } |
excel表格模板類型和調用方式
第一種 :用括號把實體類變量名稱或者數據庫字段名稱括起來
調用方法如下:
1
2
3
4
5
|
//表格的名稱為2003.xls file file= new file( "e:\\2003.xls" ); excel2jsonhelper excelhelper = getexcel2jsonhelper(); //字母表頭為在第1行,第1種模板類型 jsonarray jsonarray = excelhelper.readexcle(file, 1 , 1 ); |
第二種: 實體類變量名稱或者數據庫字段另起一行,如下兩張圖都行
調用方法如下:
1
2
3
4
5
|
//表格的名稱為2003.xls file file= new file( "e:\\2003.xls" ); excel2jsonhelper excelhelper = getexcel2jsonhelper(); //字母表頭為在第1行,第2種模板類型 jsonarray jsonarray = excelhelper.readexcle(file, 1 , 2 ); |
1
2
3
4
5
|
//表格的名稱為2003.xls file file= new file( "e:\\2003.xls" ); excel2jsonhelper excelhelper = getexcel2jsonhelper(); //字母表頭為在第2行,第2種模板類型 jsonarray jsonarray = excelhelper.readexcle(file, 2 , 2 ); |
jsonarray打印的結果
[{"index":"1","name":"李逵","jobnum":"10004","dept":"開發部","job":"android工程師"}, {"index":"2","name":"宋江","jobnum":"10001","dept":"總裁辦","job":"總裁"}]
符合我的需求,如果需要復雜的,還需要進行整理,如果有什么意見,請提出來,我及時改進…謝謝
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/allen202/article/details/54145479