提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.util; |
E |
2 |
|
|
3 |
import com.hx.exception.TipsException; |
2e06d1
|
4 |
import com.monitorjbl.xlsx.StreamingReader; |
5c5945
|
5 |
import org.apache.poi.hssf.usermodel.*; |
d9e60e
|
6 |
import org.apache.poi.ss.usermodel.*; |
C |
7 |
import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
5b623b
|
8 |
import org.apache.poi.xssf.usermodel.XSSFCell; |
C |
9 |
import org.apache.poi.xssf.usermodel.XSSFRow; |
|
10 |
import org.apache.poi.xssf.usermodel.XSSFSheet; |
|
11 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
5c5945
|
12 |
import org.springframework.web.multipart.MultipartFile; |
c6eec8
|
13 |
|
6b6bf5
|
14 |
import javax.imageio.ImageIO; |
W |
15 |
import java.awt.image.BufferedImage; |
c6eec8
|
16 |
import java.io.*; |
F |
17 |
import java.text.DateFormat; |
|
18 |
import java.text.SimpleDateFormat; |
|
19 |
import java.util.ArrayList; |
|
20 |
import java.util.Date; |
|
21 |
import java.util.List; |
|
22 |
import java.util.Map; |
5c5945
|
23 |
|
E |
24 |
|
|
25 |
/** |
cac339
|
26 |
* |
5c5945
|
27 |
* @author hjr |
E |
28 |
*/ |
|
29 |
public final class ExcelUtil { |
|
30 |
|
|
31 |
/** |
|
32 |
* @param excelName |
|
33 |
* 文件名称 |
|
34 |
* @param outPath |
|
35 |
* 保存路径 |
|
36 |
* @param headList |
|
37 |
* Excel文件Head标题集合 |
|
38 |
* @param fieldList |
|
39 |
* Excel文件Field标题集合 根据field来寻找位置填充表格 |
|
40 |
* @param dataList |
|
41 |
* Excel文件数据内容部分 |
|
42 |
* @throws Exception |
|
43 |
*/ |
|
44 |
public static String createExcel(String outPath, String excelName, |
cac339
|
45 |
String[] headList, String[] fieldList, |
C |
46 |
List<Map<String, Object>> dataList) throws Exception { |
5c5945
|
47 |
|
E |
48 |
String filePath = null; |
|
49 |
// 创建新的Excel 工作簿 |
|
50 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
|
51 |
|
|
52 |
// 在Excel工作簿中建一工作表,其名为缺省值 |
|
53 |
// 如要新建一名为"效益指标"的工作表,其语句为: |
|
54 |
// HSSFSheet sheet = workbook.createSheet("效益指标"); |
|
55 |
HSSFSheet sheet = workbook.createSheet(); |
|
56 |
// 在索引0的位置创建行(最顶端的行) |
|
57 |
HSSFRow row = sheet.createRow(0); |
|
58 |
// =============================================================== |
|
59 |
|
7f9b53
|
60 |
HSSFCell cell; |
C |
61 |
for (int i = 0; i < headList.length; i++) { |
5c5945
|
62 |
// 在索引0的位置创建单元格(左上端) |
7f9b53
|
63 |
cell = row.createCell(i); |
5c5945
|
64 |
// 定义单元格为字符串类型 |
2e06d1
|
65 |
//cell.setCellType(HSSFCell.CELL_TYPE_STRING); |
5c5945
|
66 |
// 在单元格中输入一些内容 |
E |
67 |
cell.setCellValue(headList[i]); |
|
68 |
} |
|
69 |
// =============================================================== |
|
70 |
if (dataList != null) { |
7f9b53
|
71 |
HSSFRow row_value; |
C |
72 |
Map<String, Object> dataMap; |
5c5945
|
73 |
for (int n = 0; n < dataList.size(); n++) { |
E |
74 |
// 在索引1的位置创建行 |
7f9b53
|
75 |
row_value = sheet.createRow(n + 1); |
C |
76 |
dataMap = dataList.get(n); |
5c5945
|
77 |
// =============================================================== |
E |
78 |
for (int i = 0; i < fieldList.length; i++) { |
|
79 |
// 在索引0的位置创建单元格(左上端) |
7f9b53
|
80 |
cell = row_value.createCell(i); |
5c5945
|
81 |
// 定义单元格为字符串类型 |
2e06d1
|
82 |
//cell.setCellType(HSSFCell.CELL_TYPE_STRING); |
5c5945
|
83 |
// 在单元格中输入一些内容 |
E |
84 |
cell.setCellValue(objToString(dataMap.get(fieldList[i]))); |
|
85 |
} |
|
86 |
// =============================================================== |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
// 新建一输出文件流 |
c6eec8
|
91 |
File file = SimpleTool.createFile(outPath, excelName); |
5c5945
|
92 |
FileOutputStream fOut = new FileOutputStream(file); |
E |
93 |
// 把相应的Excel 工作簿存盘 |
|
94 |
workbook.write(fOut); |
|
95 |
fOut.flush(); |
|
96 |
// 操作结束,关闭文件 |
|
97 |
fOut.close(); |
|
98 |
|
|
99 |
if(outPath.endsWith("/")){ |
|
100 |
filePath = outPath + excelName; |
|
101 |
}else{ |
|
102 |
filePath = outPath +"/"+ excelName; |
|
103 |
} |
|
104 |
return filePath; |
|
105 |
} |
|
106 |
|
db7fc9
|
107 |
/**生成临时文件 |
C |
108 |
* @param headList |
|
109 |
* Excel文件Head标题集合 |
|
110 |
* @param fieldList |
|
111 |
* Excel文件Field标题集合 根据field来寻找位置填充表格 |
|
112 |
* @param dataList |
|
113 |
* Excel文件数据内容部分 |
|
114 |
* @throws Exception |
|
115 |
*/ |
|
116 |
public static File createExcel(String[] headList, String[] fieldList, |
cac339
|
117 |
List<Map<String, Object>> dataList) throws Exception { |
b4fb02
|
118 |
File file = File.createTempFile("temp", ".xlsx"); |
db7fc9
|
119 |
try{ |
C |
120 |
// 创建新的Excel 工作簿 |
|
121 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
|
122 |
|
|
123 |
// 在Excel工作簿中建一工作表,其名为缺省值 |
|
124 |
// 如要新建一名为"效益指标"的工作表,其语句为: |
|
125 |
// HSSFSheet sheet = workbook.createSheet("效益指标"); |
|
126 |
HSSFSheet sheet = workbook.createSheet(); |
|
127 |
// 在索引0的位置创建行(最顶端的行) |
|
128 |
HSSFRow row = sheet.createRow(0); |
|
129 |
// =============================================================== |
7f9b53
|
130 |
HSSFCell cell; |
db7fc9
|
131 |
for (int i = 0; i < headList.length; i++) { |
C |
132 |
// 在索引0的位置创建单元格(左上端) |
7f9b53
|
133 |
cell = row.createCell(i); |
db7fc9
|
134 |
// 定义单元格为字符串类型 |
2e06d1
|
135 |
//cell.setCellType(HSSFCell.CELL_TYPE_STRING); |
db7fc9
|
136 |
// 在单元格中输入一些内容 |
C |
137 |
cell.setCellValue(headList[i]); |
|
138 |
} |
|
139 |
// =============================================================== |
|
140 |
if (dataList != null) { |
7f9b53
|
141 |
HSSFRow row_value; |
C |
142 |
Map<String, Object> dataMap; |
db7fc9
|
143 |
for (int n = 0; n < dataList.size(); n++) { |
C |
144 |
// 在索引1的位置创建行 |
7f9b53
|
145 |
row_value = sheet.createRow(n + 1); |
C |
146 |
dataMap = dataList.get(n); |
db7fc9
|
147 |
// =============================================================== |
C |
148 |
for (int i = 0; i < fieldList.length; i++) { |
|
149 |
// 在索引0的位置创建单元格(左上端) |
7f9b53
|
150 |
cell = row_value.createCell(i); |
db7fc9
|
151 |
// 定义单元格为字符串类型 |
2e06d1
|
152 |
//cell.setCellType(HSSFCell.CELL_TYPE_STRING); |
db7fc9
|
153 |
// 在单元格中输入一些内容 |
C |
154 |
cell.setCellValue(objToString(dataMap.get(fieldList[i]))); |
|
155 |
} |
|
156 |
// =============================================================== |
|
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
// 新建一输出文件流 |
|
161 |
FileOutputStream fOut = new FileOutputStream(file); |
|
162 |
// 把相应的Excel 工作簿存盘 |
|
163 |
workbook.write(fOut); |
|
164 |
fOut.flush(); |
|
165 |
// 操作结束,关闭文件 |
|
166 |
fOut.close(); |
|
167 |
}catch (Exception e){ |
|
168 |
|
|
169 |
}finally { |
|
170 |
file.deleteOnExit(); |
|
171 |
} |
|
172 |
return file; |
|
173 |
} |
|
174 |
|
49f28b
|
175 |
/**生成临时文件 |
C |
176 |
* @param headList |
|
177 |
* Excel文件Head标题集合 |
|
178 |
* @param fieldList |
|
179 |
* Excel文件Field标题集合 根据field来寻找位置填充表格 |
|
180 |
* @param dataList |
|
181 |
* Excel文件数据内容部分 |
|
182 |
* @throws Exception |
|
183 |
*/ |
|
184 |
public static File createExcel(String[] headList, String[] fieldList, List<Map<String, Object>> dataList |
|
185 |
,Integer height,Integer width) throws Exception { |
|
186 |
File file = File.createTempFile("temp", ".xls"); |
|
187 |
try{ |
|
188 |
|
|
189 |
if(height == null){ |
|
190 |
height = 450; |
|
191 |
} |
|
192 |
if(width == null){ |
|
193 |
width = 5000; |
|
194 |
} |
|
195 |
// 创建新的Excel 工作簿 |
|
196 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
|
197 |
|
|
198 |
//合并的单元格样式 |
|
199 |
HSSFCellStyle boderStyle = workbook.createCellStyle(); |
|
200 |
//垂直居中 |
2e06d1
|
201 |
boderStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
C |
202 |
boderStyle.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式 |
49f28b
|
203 |
|
C |
204 |
// 在Excel工作簿中建一工作表,其名为缺省值 |
|
205 |
// 如要新建一名为"效益指标"的工作表,其语句为: |
|
206 |
// HSSFSheet sheet = workbook.createSheet("效益指标"); |
|
207 |
HSSFSheet sheet = workbook.createSheet(); |
|
208 |
// 在索引0的位置创建行(最顶端的行) |
|
209 |
HSSFRow row = sheet.createRow(0); |
|
210 |
// =============================================================== |
7f9b53
|
211 |
HSSFCell cell; |
49f28b
|
212 |
for (int i = 0; i < headList.length; i++) { |
C |
213 |
//高度 |
|
214 |
row.setHeight(height.shortValue()); |
|
215 |
sheet.setColumnWidth(i,width); |
|
216 |
// 在索引0的位置创建单元格(左上端) |
7f9b53
|
217 |
cell = row.createCell(i); |
49f28b
|
218 |
// 定义单元格为字符串类型 |
2e06d1
|
219 |
//cell.setCellType(HSSFCell.CELL_TYPE_STRING); |
49f28b
|
220 |
// 在单元格中输入一些内容 |
C |
221 |
cell.setCellValue(headList[i]); |
|
222 |
cell.setCellStyle(boderStyle); |
|
223 |
} |
|
224 |
// =============================================================== |
|
225 |
if (dataList != null) { |
7f9b53
|
226 |
HSSFRow row_value; |
C |
227 |
Map<String, Object> dataMap; |
49f28b
|
228 |
for (int n = 0; n < dataList.size(); n++) { |
C |
229 |
// 在索引1的位置创建行 |
7f9b53
|
230 |
row_value = sheet.createRow(n + 1); |
49f28b
|
231 |
row_value.setHeight(height.shortValue()); |
7f9b53
|
232 |
dataMap = dataList.get(n); |
49f28b
|
233 |
// =============================================================== |
C |
234 |
for (int i = 0; i < fieldList.length; i++) { |
|
235 |
// 在索引0的位置创建单元格(左上端) |
|
236 |
sheet.setColumnWidth(i,width); |
7f9b53
|
237 |
cell = row_value.createCell(i); |
49f28b
|
238 |
// 定义单元格为字符串类型 |
2e06d1
|
239 |
//cell.setCellType(HSSFCell.CELL_TYPE_STRING); |
49f28b
|
240 |
// 在单元格中输入一些内容 |
C |
241 |
cell.setCellValue(objToString(dataMap.get(fieldList[i]))); |
|
242 |
cell.setCellStyle(boderStyle); |
|
243 |
} |
|
244 |
// =============================================================== |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
// 新建一输出文件流 |
|
249 |
FileOutputStream fOut = new FileOutputStream(file); |
|
250 |
// 把相应的Excel 工作簿存盘 |
|
251 |
workbook.write(fOut); |
|
252 |
fOut.flush(); |
|
253 |
// 操作结束,关闭文件 |
|
254 |
fOut.close(); |
|
255 |
}catch (Exception e){ |
|
256 |
|
|
257 |
}finally { |
|
258 |
file.deleteOnExit(); |
|
259 |
} |
|
260 |
return file; |
|
261 |
} |
|
262 |
|
1b1815
|
263 |
/**无限制行数生成ecxel,生成临时文件 |
C |
264 |
* @param headList |
|
265 |
* Excel文件Head标题集合 |
|
266 |
* @param fieldList |
|
267 |
* Excel文件Field标题集合 根据field来寻找位置填充表格 |
|
268 |
* @param dataList |
|
269 |
* Excel文件数据内容部分 |
|
270 |
* @param height 单元格高度,默认450 |
|
271 |
* @param width 单元格宽度,默认5000 |
|
272 |
* @throws Exception |
|
273 |
*/ |
|
274 |
public static File createXSSExcel(String[] headList, String[] fieldList, List<Map<String, Object>> dataList |
|
275 |
,Integer height,Integer width) throws Exception { |
|
276 |
File file = File.createTempFile("temp", ".xls"); |
|
277 |
try{ |
|
278 |
|
|
279 |
if(height == null){ |
|
280 |
height = 450; |
|
281 |
} |
|
282 |
if(width == null){ |
|
283 |
width = 5000; |
|
284 |
} |
|
285 |
// 创建新的Excel 工作簿 |
d9e60e
|
286 |
//XSSFWorkbook workbook = new XSSFWorkbook(); |
C |
287 |
SXSSFWorkbook workbook = new SXSSFWorkbook(1000); |
1b1815
|
288 |
|
C |
289 |
//合并的单元格样式 |
d9e60e
|
290 |
CellStyle boderStyle = workbook.createCellStyle(); |
1b1815
|
291 |
//垂直居中 |
2e06d1
|
292 |
boderStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
C |
293 |
boderStyle.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式 |
1b1815
|
294 |
|
C |
295 |
// 在Excel工作簿中建一工作表,其名为缺省值 |
|
296 |
// 如要新建一名为"效益指标"的工作表,其语句为: |
|
297 |
// HSSFSheet sheet = workbook.createSheet("效益指标"); |
d9e60e
|
298 |
Sheet sheet = workbook.createSheet(); |
1b1815
|
299 |
// 在索引0的位置创建行(最顶端的行) |
d9e60e
|
300 |
Row row = sheet.createRow(0); |
1b1815
|
301 |
// =============================================================== |
d9e60e
|
302 |
Cell cell; |
1b1815
|
303 |
for (int i = 0; i < headList.length; i++) { |
C |
304 |
//高度 |
|
305 |
row.setHeight(height.shortValue()); |
|
306 |
sheet.setColumnWidth(i,width); |
|
307 |
// 在索引0的位置创建单元格(左上端) |
7f9b53
|
308 |
cell = row.createCell(i); |
1b1815
|
309 |
// 定义单元格为字符串类型 |
2e06d1
|
310 |
//cell.setCellType(HSSFCell.CELL_TYPE_STRING); |
1b1815
|
311 |
// 在单元格中输入一些内容 |
C |
312 |
cell.setCellValue(headList[i]); |
|
313 |
cell.setCellStyle(boderStyle); |
|
314 |
} |
|
315 |
// =============================================================== |
|
316 |
if (dataList != null) { |
d9e60e
|
317 |
Row row_value; |
7f9b53
|
318 |
Map<String, Object> dataMap; |
1b1815
|
319 |
for (int n = 0; n < dataList.size(); n++) { |
C |
320 |
// 在索引1的位置创建行 |
7f9b53
|
321 |
row_value = sheet.createRow(n + 1); |
1b1815
|
322 |
row_value.setHeight(height.shortValue()); |
7f9b53
|
323 |
dataMap = dataList.get(n); |
1b1815
|
324 |
// =============================================================== |
C |
325 |
for (int i = 0; i < fieldList.length; i++) { |
|
326 |
// 在索引0的位置创建单元格(左上端) |
|
327 |
sheet.setColumnWidth(i,width); |
7f9b53
|
328 |
cell = row_value.createCell(i); |
1b1815
|
329 |
// 定义单元格为字符串类型 |
2e06d1
|
330 |
//cell.setCellType(HSSFCell.CELL_TYPE_STRING); |
1b1815
|
331 |
// 在单元格中输入一些内容 |
C |
332 |
cell.setCellValue(objToString(dataMap.get(fieldList[i]))); |
|
333 |
cell.setCellStyle(boderStyle); |
|
334 |
} |
|
335 |
// =============================================================== |
|
336 |
} |
|
337 |
} |
|
338 |
|
|
339 |
// 新建一输出文件流 |
|
340 |
FileOutputStream fOut = new FileOutputStream(file); |
|
341 |
// 把相应的Excel 工作簿存盘 |
|
342 |
workbook.write(fOut); |
|
343 |
fOut.flush(); |
|
344 |
// 操作结束,关闭文件 |
|
345 |
fOut.close(); |
|
346 |
}catch (Exception e){ |
|
347 |
|
|
348 |
}finally { |
|
349 |
file.deleteOnExit(); |
|
350 |
} |
|
351 |
return file; |
|
352 |
} |
|
353 |
|
db7fc9
|
354 |
|
5c5945
|
355 |
private static String objToString(Object obj) { |
E |
356 |
if (obj == null) { |
|
357 |
return ""; |
|
358 |
} else { |
|
359 |
if (obj instanceof String) { |
|
360 |
return (String) obj; |
|
361 |
} else if (obj instanceof Date) { |
|
362 |
return null;// DateUtil.dateToString((Date) |
cac339
|
363 |
// obj,DateUtil.DATESTYLE_SHORT_EX); |
5c5945
|
364 |
} else { |
E |
365 |
return obj.toString(); |
|
366 |
} |
|
367 |
} |
|
368 |
} |
|
369 |
|
|
370 |
/** |
c6eec8
|
371 |
* 读取Excel数据 |
F |
372 |
* @param file |
|
373 |
* @param header |
|
374 |
* @return |
|
375 |
* @throws Exception |
|
376 |
*/ |
|
377 |
public static List<List<String>> readExcelData(MultipartFile file, boolean header) throws Exception { |
|
378 |
|
|
379 |
String fileName = file.getOriginalFilename(); |
|
380 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
|
381 |
throw new TipsException("上传文件格式不正确"); |
|
382 |
} |
|
383 |
|
|
384 |
//判断不同格式处理方法不同 |
|
385 |
if(fileName.matches("^.+\\.(?i)(xls)$")){ |
|
386 |
//xls格式使用HSSF |
|
387 |
return readExcelByeFileData(file, header); |
|
388 |
}else{ |
|
389 |
//xlsx格式使用XSSF |
|
390 |
return readExcelByeFileDataToXSSF(file, header); |
|
391 |
} |
|
392 |
} |
|
393 |
|
|
394 |
/** |
5c5945
|
395 |
* 读取 Excel文件内容 |
E |
396 |
* |
|
397 |
* @param file |
|
398 |
* @param header 是否包括表头 |
|
399 |
* @return |
|
400 |
* @throws Exception |
|
401 |
*/ |
|
402 |
public static List<List<String>> readExcelByeFileData(MultipartFile file, boolean header) throws Exception { |
|
403 |
|
|
404 |
String fileName = file.getOriginalFilename(); |
|
405 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
|
406 |
throw new TipsException("上传文件格式不正确"); |
|
407 |
} |
|
408 |
|
|
409 |
// 结果集 |
|
410 |
List<List<String>> list = new ArrayList<>(); |
|
411 |
|
|
412 |
HSSFWorkbook hssfworkbook = new HSSFWorkbook(file.getInputStream()); |
|
413 |
|
|
414 |
// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 |
|
415 |
for(int s=0;s<hssfworkbook.getNumberOfSheets();s++) { |
|
416 |
HSSFSheet hssfsheet = hssfworkbook.getSheetAt(s); |
|
417 |
int col = 0; |
|
418 |
// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 去除标题 |
|
419 |
for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { |
|
420 |
HSSFRow hssfrow = hssfsheet.getRow(j); |
|
421 |
if(hssfrow!=null){ |
|
422 |
if(j == 0) { |
|
423 |
col = hssfrow.getPhysicalNumberOfCells(); |
|
424 |
if(!header) { |
|
425 |
//不包括表头 |
|
426 |
continue; |
|
427 |
} |
|
428 |
} |
|
429 |
// 单行数据 |
|
430 |
List<String> arrayString = new ArrayList<>(); |
|
431 |
for (int i = 0; i < col; i++) { |
|
432 |
HSSFCell cell = hssfrow.getCell(i); |
|
433 |
if (cell == null) { |
|
434 |
arrayString.add(""); |
2e06d1
|
435 |
} else if (cell.getCellType() == CellType.NUMERIC) { |
5c5945
|
436 |
// arrayString[i] = new Double(cell.getNumericCellValue()).toString(); |
2e06d1
|
437 |
if (CellType.NUMERIC == cell.getCellType()) { |
5c5945
|
438 |
short format = cell.getCellStyle().getDataFormat(); |
E |
439 |
if(format == 14 || format == 31 || format == 57 || format == 58){ |
|
440 |
//日期(中文时间格式的) |
|
441 |
Date d = cell.getDateCellValue(); |
|
442 |
DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
443 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
444 |
arrayString.add(formater.format(d)); |
|
445 |
//arrayString[i] = formater.format(d); |
|
446 |
}else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
447 |
Date d = cell.getDateCellValue(); |
|
448 |
//DateFormat formater = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); |
|
449 |
DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
450 |
arrayString.add(formater.format(d)); |
|
451 |
//arrayString[i] = formater.format(d); |
|
452 |
} else { |
2e06d1
|
453 |
if(CellType.STRING == cell.getCellType()){ |
5c5945
|
454 |
arrayString.add(cell.getStringCellValue()); |
E |
455 |
//arrayString[i] =cell.getStringCellValue(); |
2e06d1
|
456 |
}else if(CellType.FORMULA==cell.getCellType()){ |
5c5945
|
457 |
arrayString.add(cell.getCellFormula()); |
E |
458 |
//arrayString[i] =cell.getCellFormula(); |
2e06d1
|
459 |
}else if(CellType.NUMERIC== cell.getCellType()){ |
5c5945
|
460 |
HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); |
E |
461 |
arrayString.add(dataFormatter.formatCellValue(cell)); |
|
462 |
//arrayString[i] =dataFormatter.formatCellValue(cell); |
|
463 |
} |
|
464 |
} |
|
465 |
} |
2e06d1
|
466 |
} else if(cell.getCellType() == CellType.BLANK){ |
5c5945
|
467 |
arrayString.add(""); |
E |
468 |
//arrayString[i] = ""; |
|
469 |
} else { // 如果EXCEL表格中的数据类型为字符串型 |
2e06d1
|
470 |
if(cell.getCellType() != CellType.BOOLEAN){ |
b77bfb
|
471 |
arrayString.add(cell.getStringCellValue().trim()); |
F |
472 |
}else{ |
|
473 |
arrayString.add(cell.getBooleanCellValue() ? "TRUE" : "FALSE"); |
|
474 |
} |
5c5945
|
475 |
//arrayString[i] = cell.getStringCellValue().trim(); |
E |
476 |
} |
|
477 |
} |
|
478 |
list.add(arrayString); |
|
479 |
} |
|
480 |
} |
|
481 |
} |
|
482 |
return list; |
|
483 |
} |
|
484 |
|
c6eec8
|
485 |
/** |
F |
486 |
* 读取 Excel文件内容 |
|
487 |
* |
|
488 |
* @param file |
|
489 |
* @param header 是否包括表头 |
|
490 |
* @return |
|
491 |
* @throws Exception |
|
492 |
*/ |
|
493 |
public static List<List<String>> readExcelByeFileDataToXSSF(MultipartFile file, boolean header) throws Exception { |
|
494 |
|
|
495 |
String fileName = file.getOriginalFilename(); |
|
496 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
|
497 |
throw new TipsException("上传文件格式不正确"); |
|
498 |
} |
|
499 |
|
|
500 |
// 结果集 |
|
501 |
List<List<String>> list = new ArrayList<>(); |
|
502 |
|
|
503 |
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file.getInputStream()); |
|
504 |
|
|
505 |
// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 |
|
506 |
for(int s=0;s<xssfWorkbook.getNumberOfSheets();s++) { |
|
507 |
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(s); |
|
508 |
int col = 0; |
|
509 |
// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 去除标题 |
|
510 |
for (int j = 0; j < xssfSheet.getPhysicalNumberOfRows(); j++) { |
|
511 |
XSSFRow xssfrow = xssfSheet.getRow(j); |
|
512 |
if(xssfrow!=null){ |
|
513 |
if(j == 0) { |
|
514 |
col = xssfrow.getPhysicalNumberOfCells(); |
|
515 |
if(!header) { |
|
516 |
//不包括表头 |
|
517 |
continue; |
|
518 |
} |
|
519 |
} |
|
520 |
// 单行数据 |
|
521 |
List<String> arrayString = new ArrayList<>(); |
|
522 |
for (int i = 0; i < col; i++) { |
|
523 |
XSSFCell cell = xssfrow.getCell(i); |
|
524 |
if (cell == null) { |
|
525 |
arrayString.add(""); |
2e06d1
|
526 |
} else if (cell.getCellType() == CellType.NUMERIC) { |
c6eec8
|
527 |
// arrayString[i] = new Double(cell.getNumericCellValue()).toString(); |
2e06d1
|
528 |
if (CellType.NUMERIC == cell.getCellType()) { |
c6eec8
|
529 |
short format = cell.getCellStyle().getDataFormat(); |
F |
530 |
if(format == 14 || format == 31 || format == 57 || format == 58){ |
|
531 |
//日期(中文时间格式的) |
|
532 |
Date d = cell.getDateCellValue(); |
|
533 |
DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
534 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
535 |
arrayString.add(formater.format(d)); |
|
536 |
//arrayString[i] = formater.format(d); |
|
537 |
}else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
538 |
Date d = cell.getDateCellValue(); |
|
539 |
//DateFormat formater = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); |
|
540 |
DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
541 |
arrayString.add(formater.format(d)); |
|
542 |
//arrayString[i] = formater.format(d); |
|
543 |
} else { |
2e06d1
|
544 |
if(CellType.STRING == cell.getCellType()){ |
c6eec8
|
545 |
arrayString.add(cell.getStringCellValue()); |
F |
546 |
//arrayString[i] =cell.getStringCellValue(); |
2e06d1
|
547 |
}else if(CellType.FORMULA==cell.getCellType()){ |
c6eec8
|
548 |
arrayString.add(cell.getCellFormula()); |
F |
549 |
//arrayString[i] =cell.getCellFormula(); |
2e06d1
|
550 |
}else if(CellType.NUMERIC== cell.getCellType()){ |
c6eec8
|
551 |
HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); |
F |
552 |
arrayString.add(dataFormatter.formatCellValue(cell)); |
|
553 |
//arrayString[i] =dataFormatter.formatCellValue(cell); |
|
554 |
} |
|
555 |
} |
|
556 |
} |
2e06d1
|
557 |
} else if(cell.getCellType() == CellType.BLANK){ |
c6eec8
|
558 |
arrayString.add(""); |
F |
559 |
//arrayString[i] = ""; |
|
560 |
} else { // 如果EXCEL表格中的数据类型为字符串型 |
2e06d1
|
561 |
if(cell.getCellType() != CellType.BOOLEAN){ |
b77bfb
|
562 |
arrayString.add(cell.getStringCellValue().trim()); |
F |
563 |
}else{ |
|
564 |
arrayString.add(cell.getBooleanCellValue() ? "TRUE" : "FALSE"); |
|
565 |
} |
|
566 |
|
|
567 |
|
c6eec8
|
568 |
//arrayString[i] = cell.getStringCellValue().trim(); |
F |
569 |
} |
|
570 |
} |
|
571 |
list.add(arrayString); |
|
572 |
} |
|
573 |
} |
|
574 |
} |
|
575 |
return list; |
|
576 |
} |
|
577 |
|
cac339
|
578 |
|
C |
579 |
/**判断excel的版本*/ |
2e06d1
|
580 |
public static Workbook create(InputStream inp) throws IOException { |
C |
581 |
//这样写 excel 能兼容03和07 |
|
582 |
return WorkbookFactory.create(inp); |
cac339
|
583 |
} |
C |
584 |
|
|
585 |
/**读取excel文件,兼容2003和2007 |
|
586 |
* 通过流读取Excel文件 |
|
587 |
* @return |
|
588 |
* @throws Exception |
|
589 |
*/ |
3f4072
|
590 |
public static List<List<String>> getExcelDataCompatible(File file,boolean header) throws Exception { |
C |
591 |
try { |
|
592 |
|
|
593 |
String fileName = file.getName(); |
|
594 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
|
595 |
throw new TipsException("上传文件格式不正确"); |
|
596 |
} |
|
597 |
|
|
598 |
// 结果集 |
|
599 |
List<List<String>> list = new ArrayList<>(); |
|
600 |
Workbook book = create(new BufferedInputStream(new FileInputStream(file))); |
|
601 |
|
|
602 |
// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 |
2e06d1
|
603 |
Sheet hssfsheet; |
C |
604 |
Row hssfrow; |
|
605 |
List<String> arrayString; |
|
606 |
Cell cell; |
|
607 |
short format; |
|
608 |
Date d; |
|
609 |
DateFormat formater; |
|
610 |
HSSFDataFormatter dataFormatter; |
3f4072
|
611 |
for(int s=0;s<book.getNumberOfSheets();s++) { |
2e06d1
|
612 |
hssfsheet = book.getSheetAt(s); |
3f4072
|
613 |
int col = 0; |
C |
614 |
// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 去除标题 |
|
615 |
for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { |
2e06d1
|
616 |
hssfrow = hssfsheet.getRow(j); |
3f4072
|
617 |
if(hssfrow!=null){ |
C |
618 |
if(j == 0) { |
|
619 |
col = hssfrow.getPhysicalNumberOfCells(); |
|
620 |
if(!header) { |
|
621 |
//不包括表头 |
|
622 |
continue; |
|
623 |
} |
|
624 |
} |
|
625 |
// 单行数据 |
2e06d1
|
626 |
arrayString = new ArrayList<>(); |
3f4072
|
627 |
for (int i = 0; i < col; i++) { |
2e06d1
|
628 |
cell = hssfrow.getCell(i); |
3f4072
|
629 |
if (cell == null) { |
C |
630 |
arrayString.add(""); |
2e06d1
|
631 |
} else if (cell.getCellType() == CellType.NUMERIC) { |
3f4072
|
632 |
// arrayString[i] = new Double(cell.getNumericCellValue()).toString(); |
2e06d1
|
633 |
if (CellType.NUMERIC == cell.getCellType()) { |
C |
634 |
format = cell.getCellStyle().getDataFormat(); |
3f4072
|
635 |
if(format == 14 || format == 31 || format == 57 || format == 58){ |
C |
636 |
//日期(中文时间格式的) |
2e06d1
|
637 |
d = cell.getDateCellValue(); |
C |
638 |
formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
3f4072
|
639 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
C |
640 |
arrayString.add(formater.format(d)); |
|
641 |
//arrayString[i] = formater.format(d); |
|
642 |
}else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
2e06d1
|
643 |
d = cell.getDateCellValue(); |
3f4072
|
644 |
//DateFormat formater = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); |
2e06d1
|
645 |
formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
3f4072
|
646 |
arrayString.add(formater.format(d)); |
C |
647 |
//arrayString[i] = formater.format(d); |
|
648 |
} else { |
2e06d1
|
649 |
if(CellType.STRING == cell.getCellType()){ |
3f4072
|
650 |
arrayString.add(cell.getStringCellValue()); |
C |
651 |
//arrayString[i] =cell.getStringCellValue(); |
2e06d1
|
652 |
}else if(CellType.FORMULA==cell.getCellType()){ |
3f4072
|
653 |
arrayString.add(cell.getCellFormula()); |
C |
654 |
//arrayString[i] =cell.getCellFormula(); |
2e06d1
|
655 |
}else if(CellType.NUMERIC== cell.getCellType()){ |
C |
656 |
dataFormatter = new HSSFDataFormatter(); |
3f4072
|
657 |
arrayString.add(dataFormatter.formatCellValue(cell)); |
C |
658 |
//arrayString[i] =dataFormatter.formatCellValue(cell); |
|
659 |
} |
|
660 |
} |
|
661 |
} |
2e06d1
|
662 |
} else if(cell.getCellType() == CellType.BLANK){ |
3f4072
|
663 |
arrayString.add(""); |
C |
664 |
//arrayString[i] = ""; |
|
665 |
} else { // 如果EXCEL表格中的数据类型为字符串型 |
|
666 |
arrayString.add(cell.getStringCellValue().trim()); |
|
667 |
//arrayString[i] = cell.getStringCellValue().trim(); |
|
668 |
} |
|
669 |
} |
|
670 |
list.add(arrayString); |
|
671 |
} |
|
672 |
} |
|
673 |
} |
|
674 |
return list; |
|
675 |
} catch (Exception e) { |
|
676 |
e.printStackTrace(); |
|
677 |
} |
|
678 |
return null; |
|
679 |
} |
2e06d1
|
680 |
|
3f4072
|
681 |
/**读取excel文件,兼容2003和2007 |
C |
682 |
* 通过流读取Excel文件 |
|
683 |
* @return |
|
684 |
* @throws Exception |
|
685 |
*/ |
cac339
|
686 |
public static List<List<String>> getExcelDataCompatible(MultipartFile file,boolean header) throws Exception { |
C |
687 |
try { |
|
688 |
|
|
689 |
String fileName = file.getOriginalFilename(); |
|
690 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
|
691 |
throw new TipsException("上传文件格式不正确"); |
|
692 |
} |
|
693 |
|
|
694 |
// 结果集 |
|
695 |
List<List<String>> list = new ArrayList<>(); |
|
696 |
Workbook book = create(new BufferedInputStream(file.getInputStream())); |
|
697 |
|
2e06d1
|
698 |
Sheet hssfsheet; |
C |
699 |
Row hssfrow; |
|
700 |
List<String> arrayString; |
|
701 |
Cell cell; |
|
702 |
short format; |
|
703 |
Date d; |
|
704 |
DateFormat formater; |
|
705 |
HSSFDataFormatter dataFormatter; |
cac339
|
706 |
// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 |
2e06d1
|
707 |
for(int s=0;s<book.getNumberOfSheets();s++) { |
C |
708 |
hssfsheet = book.getSheetAt(s); |
|
709 |
int col = 0; |
|
710 |
// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 去除标题 |
|
711 |
for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { |
|
712 |
hssfrow = hssfsheet.getRow(j); |
|
713 |
if(hssfrow!=null){ |
|
714 |
if(j == 0) { |
|
715 |
col = hssfrow.getPhysicalNumberOfCells(); |
|
716 |
if(!header) { |
|
717 |
//不包括表头 |
|
718 |
continue; |
|
719 |
} |
|
720 |
} |
|
721 |
// 单行数据 |
|
722 |
arrayString = new ArrayList<>(); |
|
723 |
for (int i = 0; i < col; i++) { |
|
724 |
cell = hssfrow.getCell(i); |
|
725 |
if (cell == null) { |
|
726 |
arrayString.add(""); |
|
727 |
} else if (cell.getCellType() == CellType.NUMERIC) { |
|
728 |
// arrayString[i] = new Double(cell.getNumericCellValue()).toString(); |
|
729 |
if (CellType.NUMERIC == cell.getCellType()) { |
|
730 |
format = cell.getCellStyle().getDataFormat(); |
|
731 |
if(format == 14 || format == 31 || format == 57 || format == 58){ |
|
732 |
//日期(中文时间格式的) |
|
733 |
d = cell.getDateCellValue(); |
|
734 |
formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
735 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
736 |
arrayString.add(formater.format(d)); |
|
737 |
//arrayString[i] = formater.format(d); |
|
738 |
}else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
739 |
d = cell.getDateCellValue(); |
|
740 |
//DateFormat formater = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); |
|
741 |
formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
742 |
arrayString.add(formater.format(d)); |
|
743 |
//arrayString[i] = formater.format(d); |
|
744 |
} else { |
|
745 |
if(CellType.STRING == cell.getCellType()){ |
|
746 |
arrayString.add(cell.getStringCellValue()); |
|
747 |
//arrayString[i] =cell.getStringCellValue(); |
|
748 |
}else if(CellType.FORMULA==cell.getCellType()){ |
|
749 |
arrayString.add(cell.getCellFormula()); |
|
750 |
//arrayString[i] =cell.getCellFormula(); |
|
751 |
}else if(CellType.NUMERIC== cell.getCellType()){ |
|
752 |
dataFormatter = new HSSFDataFormatter(); |
|
753 |
arrayString.add(dataFormatter.formatCellValue(cell)); |
|
754 |
//arrayString[i] =dataFormatter.formatCellValue(cell); |
|
755 |
} |
|
756 |
} |
|
757 |
} |
|
758 |
} else if(cell.getCellType() == CellType.BLANK){ |
|
759 |
arrayString.add(""); |
|
760 |
//arrayString[i] = ""; |
|
761 |
} else { // 如果EXCEL表格中的数据类型为字符串型 |
|
762 |
arrayString.add(cell.getStringCellValue().trim()); |
|
763 |
//arrayString[i] = cell.getStringCellValue().trim(); |
|
764 |
} |
|
765 |
} |
|
766 |
list.add(arrayString); |
|
767 |
} |
|
768 |
} |
|
769 |
} |
|
770 |
return list; |
|
771 |
} catch (Exception e) { |
|
772 |
e.printStackTrace(); |
|
773 |
} |
|
774 |
return null; |
|
775 |
} |
|
776 |
|
399ee0
|
777 |
/**读取excel文件,兼容2003和2007 |
C |
778 |
* 通过流读取Excel文件 |
|
779 |
* @return |
|
780 |
* @throws Exception |
|
781 |
*/ |
|
782 |
public static List<List<String>> getExcelDataCompatible(InputStream inputStream,boolean header) throws Exception { |
|
783 |
try { |
|
784 |
// 结果集 |
|
785 |
List<List<String>> list = new ArrayList<>(); |
|
786 |
Workbook book = create(new BufferedInputStream(inputStream)); |
|
787 |
|
|
788 |
Sheet hssfsheet; |
|
789 |
Row hssfrow; |
|
790 |
List<String> arrayString; |
|
791 |
Cell cell; |
|
792 |
short format; |
|
793 |
Date d; |
|
794 |
DateFormat formater; |
|
795 |
HSSFDataFormatter dataFormatter; |
|
796 |
// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 |
|
797 |
for(int s=0;s<book.getNumberOfSheets();s++) { |
|
798 |
hssfsheet = book.getSheetAt(s); |
|
799 |
int col = 0; |
|
800 |
// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 去除标题 |
|
801 |
for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { |
|
802 |
hssfrow = hssfsheet.getRow(j); |
|
803 |
if(hssfrow!=null){ |
|
804 |
if(j == 0) { |
|
805 |
col = hssfrow.getPhysicalNumberOfCells(); |
|
806 |
if(!header) { |
|
807 |
//不包括表头 |
|
808 |
continue; |
|
809 |
} |
|
810 |
} |
|
811 |
// 单行数据 |
|
812 |
arrayString = new ArrayList<>(); |
|
813 |
for (int i = 0; i < col; i++) { |
|
814 |
cell = hssfrow.getCell(i); |
|
815 |
if (cell == null) { |
|
816 |
arrayString.add(""); |
|
817 |
} else if (cell.getCellType() == CellType.NUMERIC) { |
|
818 |
// arrayString[i] = new Double(cell.getNumericCellValue()).toString(); |
|
819 |
if (CellType.NUMERIC == cell.getCellType()) { |
|
820 |
format = cell.getCellStyle().getDataFormat(); |
|
821 |
if(format == 14 || format == 31 || format == 57 || format == 58){ |
|
822 |
//日期(中文时间格式的) |
|
823 |
d = cell.getDateCellValue(); |
|
824 |
formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
825 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
826 |
arrayString.add(formater.format(d)); |
|
827 |
//arrayString[i] = formater.format(d); |
|
828 |
}else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
829 |
d = cell.getDateCellValue(); |
|
830 |
//DateFormat formater = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); |
|
831 |
formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
832 |
arrayString.add(formater.format(d)); |
|
833 |
//arrayString[i] = formater.format(d); |
|
834 |
} else { |
|
835 |
if(CellType.STRING == cell.getCellType()){ |
|
836 |
arrayString.add(cell.getStringCellValue()); |
|
837 |
//arrayString[i] =cell.getStringCellValue(); |
|
838 |
}else if(CellType.FORMULA==cell.getCellType()){ |
|
839 |
arrayString.add(cell.getCellFormula()); |
|
840 |
//arrayString[i] =cell.getCellFormula(); |
|
841 |
}else if(CellType.NUMERIC== cell.getCellType()){ |
|
842 |
dataFormatter = new HSSFDataFormatter(); |
|
843 |
arrayString.add(dataFormatter.formatCellValue(cell)); |
|
844 |
//arrayString[i] =dataFormatter.formatCellValue(cell); |
|
845 |
} |
|
846 |
} |
|
847 |
} |
|
848 |
} else if(cell.getCellType() == CellType.BLANK){ |
|
849 |
arrayString.add(""); |
|
850 |
//arrayString[i] = ""; |
|
851 |
} else { // 如果EXCEL表格中的数据类型为字符串型 |
|
852 |
arrayString.add(cell.getStringCellValue().trim()); |
|
853 |
//arrayString[i] = cell.getStringCellValue().trim(); |
|
854 |
} |
|
855 |
} |
|
856 |
list.add(arrayString); |
|
857 |
} |
|
858 |
} |
|
859 |
} |
|
860 |
return list; |
|
861 |
} catch (Exception e) { |
|
862 |
e.printStackTrace(); |
|
863 |
} |
|
864 |
return null; |
|
865 |
} |
5b623b
|
866 |
|
C |
867 |
/** |
|
868 |
* 新版读取Excel,只支持2007以上版本,也就是xslx格式 |
cd78e3
|
869 |
* 支持大数据量 |
5b623b
|
870 |
* @param file 文件 |
C |
871 |
* @return 数据 |
2e06d1
|
872 |
*/ |
5b623b
|
873 |
public static List<List<String>> readExcelData(File file){ |
C |
874 |
if(!isExcel(file)){ |
|
875 |
throw new TipsException("请上传excel的文件格式!"); |
|
876 |
} |
|
877 |
List<List<String>> listData = new ArrayList<>(); |
|
878 |
try{ |
|
879 |
//rowCacheSize 缓存到内存中的行数(默认是10) |
|
880 |
//bufferSize 读取资源时,缓存到内存的字节大小(默认是1024) |
|
881 |
//open InputStream或者XLSX格式的File(必须) |
|
882 |
Workbook book = StreamingReader.builder() |
|
883 |
.rowCacheSize(100) |
399ee0
|
884 |
.bufferSize(10240) |
5b623b
|
885 |
.open(new FileInputStream(file)); |
399ee0
|
886 |
listData =readhandle(book); |
5b623b
|
887 |
}catch (Exception e){ |
d57fc1
|
888 |
e.printStackTrace(); |
5b623b
|
889 |
throw new RuntimeException(e.getMessage()); |
d57fc1
|
890 |
} |
5b623b
|
891 |
return listData; |
d57fc1
|
892 |
} |
F |
893 |
|
5b623b
|
894 |
/** |
C |
895 |
* 新版读取Excel,只支持2007以上版本,也就是xslx格式 |
cd78e3
|
896 |
* 支持大数据量 |
5b623b
|
897 |
* @param file 文件 |
C |
898 |
* @return 数据 |
2e06d1
|
899 |
*/ |
5b623b
|
900 |
public static List<List<String>> readExcelData(MultipartFile file){ |
C |
901 |
if(!isExcel(file)){ |
|
902 |
throw new TipsException("请上传excel的文件格式!"); |
|
903 |
} |
|
904 |
List<List<String>> listData = new ArrayList<>(); |
|
905 |
try{ |
|
906 |
//rowCacheSize 缓存到内存中的行数(默认是10) |
|
907 |
//bufferSize 读取资源时,缓存到内存的字节大小(默认是1024) |
|
908 |
//open InputStream或者XLSX格式的File(必须) |
|
909 |
Workbook book = StreamingReader.builder() |
|
910 |
.rowCacheSize(100) |
399ee0
|
911 |
.bufferSize(10240) |
5b623b
|
912 |
.open(new BufferedInputStream(file.getInputStream())); |
2e06d1
|
913 |
|
399ee0
|
914 |
listData =readhandle(book); |
5b623b
|
915 |
}catch (Exception e){ |
2e06d1
|
916 |
e.printStackTrace(); |
5b623b
|
917 |
throw new RuntimeException(e.getMessage()); |
2e06d1
|
918 |
} |
5b623b
|
919 |
return listData; |
2e06d1
|
920 |
} |
C |
921 |
|
399ee0
|
922 |
/** |
C |
923 |
* 新版读取Excel,只支持2007以上版本,也就是xslx格式 |
cd78e3
|
924 |
* 支持大数据量 |
399ee0
|
925 |
* @param file 文件 |
C |
926 |
* @return 数据 |
|
927 |
*/ |
|
928 |
public static List<List<String>> readExcelData(InputStream file){ |
|
929 |
List<List<String>> listData = new ArrayList<>(); |
|
930 |
try{ |
|
931 |
//rowCacheSize 缓存到内存中的行数(默认是10) |
|
932 |
//bufferSize 读取资源时,缓存到内存的字节大小(默认是1024) |
|
933 |
//open InputStream或者XLSX格式的File(必须) |
|
934 |
Workbook book = StreamingReader.builder() |
|
935 |
.rowCacheSize(100) |
|
936 |
.bufferSize(10240) |
|
937 |
.open(file); |
|
938 |
|
|
939 |
listData =readhandle(book); |
|
940 |
}catch (Exception e){ |
|
941 |
e.printStackTrace(); |
|
942 |
throw new RuntimeException(e.getMessage()); |
|
943 |
} |
|
944 |
return listData; |
|
945 |
} |
|
946 |
|
|
947 |
/**处理数据*/ |
|
948 |
public static List<List<String>> readhandle(Workbook book){ |
|
949 |
List<List<String>> listData = new ArrayList<>(); |
|
950 |
|
|
951 |
//是否存在数据 |
|
952 |
boolean isData; |
|
953 |
List<String> arrayString; |
|
954 |
short format; |
|
955 |
Date d; |
|
956 |
DateFormat formater; |
|
957 |
Sheet sheet; |
756acb
|
958 |
Cell cell; |
399ee0
|
959 |
//遍历所有的sheet |
C |
960 |
for(int i=0;i<book.getNumberOfSheets();i++) { |
|
961 |
sheet = book.getSheetAt(i); |
756acb
|
962 |
//列数 |
C |
963 |
Integer arrange = null; |
399ee0
|
964 |
//遍历所有的行 |
C |
965 |
for (Row row : sheet) { |
756acb
|
966 |
if(row == null){ |
C |
967 |
continue; |
|
968 |
} |
399ee0
|
969 |
isData = false; |
C |
970 |
arrayString = new ArrayList<>(); |
756acb
|
971 |
if(arrange == null){ |
C |
972 |
arrange = row.getPhysicalNumberOfCells(); |
|
973 |
} |
399ee0
|
974 |
//遍历所有的列 |
756acb
|
975 |
for (int j = 0;j<arrange;j++) { |
C |
976 |
cell = row.getCell(j); |
399ee0
|
977 |
if (cell == null) { |
a1bdc4
|
978 |
arrayString.add(""); |
8b3e27
|
979 |
}else{ |
C |
980 |
if (cell.getCellType() == CellType.NUMERIC) { |
|
981 |
isData = true; |
|
982 |
format = cell.getCellStyle().getDataFormat(); |
|
983 |
if (format == 14 || format == 31 || format == 57 || format == 58) { |
|
984 |
//日期(中文时间格式的) |
|
985 |
d = cell.getDateCellValue(); |
|
986 |
formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
987 |
arrayString.add(formater.format(d)); |
|
988 |
} else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
989 |
d = cell.getDateCellValue(); |
|
990 |
formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
991 |
arrayString.add(formater.format(d)); |
399ee0
|
992 |
} else { |
8b3e27
|
993 |
if (CellType.STRING == cell.getCellType()) { |
C |
994 |
arrayString.add(cell.getStringCellValue()); |
|
995 |
} else if (CellType.FORMULA == cell.getCellType()) { |
|
996 |
arrayString.add(cell.getCellFormula()); |
|
997 |
} else { |
|
998 |
arrayString.add(cell.getStringCellValue().trim()); |
|
999 |
} |
399ee0
|
1000 |
} |
8b3e27
|
1001 |
} else if (cell.getCellType() == CellType.BLANK) { |
C |
1002 |
arrayString.add(""); |
|
1003 |
} else { // 如果EXCEL表格中的数据类型为字符串型 |
|
1004 |
isData = true; |
|
1005 |
arrayString.add(cell.getStringCellValue().trim()); |
399ee0
|
1006 |
} |
C |
1007 |
} |
|
1008 |
} |
|
1009 |
if (isData) { |
|
1010 |
listData.add(arrayString); |
|
1011 |
} |
|
1012 |
} |
|
1013 |
} |
|
1014 |
return listData; |
|
1015 |
} |
|
1016 |
|
cac339
|
1017 |
|
e7c3ff
|
1018 |
/** |
C |
1019 |
* p判断是否excel文件 |
|
1020 |
* @param file |
|
1021 |
* @return |
|
1022 |
*/ |
|
1023 |
public static boolean isExcel(MultipartFile file){ |
5b623b
|
1024 |
return isExcel(file.getOriginalFilename()); |
C |
1025 |
} |
|
1026 |
|
|
1027 |
|
|
1028 |
/** |
|
1029 |
* p判断是否excel文件 |
|
1030 |
* @param file |
|
1031 |
* @return |
|
1032 |
*/ |
|
1033 |
public static boolean isExcel(File file){ |
|
1034 |
return isExcel(file.getName()); |
|
1035 |
} |
|
1036 |
|
|
1037 |
|
|
1038 |
/**判断文件格式是不是excel*/ |
|
1039 |
public static boolean isExcel(String fileName){ |
e7c3ff
|
1040 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
C |
1041 |
return false; |
|
1042 |
} |
|
1043 |
return true; |
|
1044 |
} |
6b6bf5
|
1045 |
|
W |
1046 |
public static File createExcelByImg(String[] headList, String[] fieldList, List<Map<String, Object>> dataList, Integer height, Integer width) throws Exception { |
|
1047 |
File file = File.createTempFile("temp", ".xls"); |
|
1048 |
FileOutputStream fileOut = null; |
|
1049 |
BufferedImage bufferImg = null; |
|
1050 |
|
|
1051 |
try { |
|
1052 |
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); |
|
1053 |
if (height == null) { |
|
1054 |
height = 450; |
|
1055 |
} |
|
1056 |
|
|
1057 |
if (width == null) { |
|
1058 |
width = 1000; |
|
1059 |
} |
|
1060 |
|
|
1061 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
|
1062 |
HSSFCellStyle boderStyle = workbook.createCellStyle(); |
2e06d1
|
1063 |
boderStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
C |
1064 |
boderStyle.setAlignment(HorizontalAlignment.CENTER); |
6b6bf5
|
1065 |
HSSFSheet sheet = workbook.createSheet(); |
W |
1066 |
HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); |
|
1067 |
HSSFRow row = sheet.createRow(0); |
|
1068 |
|
|
1069 |
HSSFCell anchor; |
|
1070 |
for(int i = 0; i < headList.length; ++i) { |
|
1071 |
row.setHeight(height.shortValue()); |
|
1072 |
sheet.setColumnWidth(i, width); |
|
1073 |
anchor = row.createCell(i); |
|
1074 |
anchor.setCellValue(headList[i]); |
|
1075 |
anchor.setCellStyle(boderStyle); |
|
1076 |
} |
|
1077 |
|
|
1078 |
HSSFRow row_value = null; |
|
1079 |
anchor = null; |
|
1080 |
HSSFCell cell = null; |
|
1081 |
if (dataList != null) { |
|
1082 |
for(int n = 0; n < dataList.size(); ++n) { |
|
1083 |
row_value = sheet.createRow(n + 1); |
|
1084 |
row_value.setHeight(height.shortValue()); |
|
1085 |
Map<String, Object> dataMap = (Map)dataList.get(n); |
|
1086 |
|
|
1087 |
for(int i = 0; i < fieldList.length; ++i) { |
|
1088 |
sheet.setColumnWidth(i, width); |
|
1089 |
cell = row_value.createCell(i); |
|
1090 |
Object value = dataMap.get(fieldList[i]); |
|
1091 |
if (value != null && "class java.io.File".equals(value.getClass().toString())) { |
|
1092 |
File file2 = (File)value; |
|
1093 |
if (file2 == null) { |
|
1094 |
cell.setCellValue(""); |
|
1095 |
} else { |
|
1096 |
bufferImg = ImageIO.read(file2); |
|
1097 |
ImageIO.write(bufferImg, "jpg", byteArrayOut); |
|
1098 |
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 1023, 255, (short)i, n + 1, (short)i, n + 1); |
2e06d1
|
1099 |
anchor1.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); |
6b6bf5
|
1100 |
patriarch.createPicture(anchor1, workbook.addPicture(byteArrayOut.toByteArray(), 5)); |
W |
1101 |
} |
|
1102 |
} else { |
|
1103 |
cell.setCellValue(objToString(dataMap.get(fieldList[i]))); |
|
1104 |
cell.setCellStyle(boderStyle); |
|
1105 |
} |
|
1106 |
} |
|
1107 |
} |
|
1108 |
} |
|
1109 |
|
|
1110 |
FileOutputStream fOut = new FileOutputStream(file); |
|
1111 |
workbook.write(fOut); |
|
1112 |
fOut.flush(); |
|
1113 |
fOut.close(); |
|
1114 |
} catch (Exception var25) { |
|
1115 |
var25.printStackTrace(); |
|
1116 |
} finally { |
|
1117 |
file.deleteOnExit(); |
|
1118 |
} |
|
1119 |
|
|
1120 |
return file; |
2e06d1
|
1121 |
} |
C |
1122 |
|
cac339
|
1123 |
} |