提交 | 用户 | 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.*; |
2e06d1
|
6 |
import org.apache.poi.ooxml.POIXMLDocument; |
cac339
|
7 |
import org.apache.poi.openxml4j.opc.OPCPackage; |
C |
8 |
import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
d9e60e
|
9 |
import org.apache.poi.ss.usermodel.*; |
C |
10 |
import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
1b1815
|
11 |
import org.apache.poi.xssf.usermodel.*; |
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 |
|
C |
682 |
|
|
683 |
|
|
684 |
/**读取excel文件,兼容2003和2007 |
|
685 |
* 通过流读取Excel文件 |
|
686 |
* @return |
|
687 |
* @throws Exception |
|
688 |
*/ |
cac339
|
689 |
public static List<List<String>> getExcelDataCompatible(MultipartFile file,boolean header) throws Exception { |
C |
690 |
try { |
|
691 |
|
|
692 |
String fileName = file.getOriginalFilename(); |
|
693 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
|
694 |
throw new TipsException("上传文件格式不正确"); |
|
695 |
} |
|
696 |
|
|
697 |
// 结果集 |
|
698 |
List<List<String>> list = new ArrayList<>(); |
|
699 |
Workbook book = create(new BufferedInputStream(file.getInputStream())); |
|
700 |
|
2e06d1
|
701 |
Sheet hssfsheet; |
C |
702 |
Row hssfrow; |
|
703 |
List<String> arrayString; |
|
704 |
Cell cell; |
|
705 |
short format; |
|
706 |
Date d; |
|
707 |
DateFormat formater; |
|
708 |
HSSFDataFormatter dataFormatter; |
cac339
|
709 |
// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 |
2e06d1
|
710 |
for(int s=0;s<book.getNumberOfSheets();s++) { |
C |
711 |
hssfsheet = book.getSheetAt(s); |
|
712 |
int col = 0; |
|
713 |
// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 去除标题 |
|
714 |
for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { |
|
715 |
hssfrow = hssfsheet.getRow(j); |
|
716 |
if(hssfrow!=null){ |
|
717 |
if(j == 0) { |
|
718 |
col = hssfrow.getPhysicalNumberOfCells(); |
|
719 |
if(!header) { |
|
720 |
//不包括表头 |
|
721 |
continue; |
|
722 |
} |
|
723 |
} |
|
724 |
// 单行数据 |
|
725 |
arrayString = new ArrayList<>(); |
|
726 |
for (int i = 0; i < col; i++) { |
|
727 |
cell = hssfrow.getCell(i); |
|
728 |
if (cell == null) { |
|
729 |
arrayString.add(""); |
|
730 |
} else if (cell.getCellType() == CellType.NUMERIC) { |
|
731 |
// arrayString[i] = new Double(cell.getNumericCellValue()).toString(); |
|
732 |
if (CellType.NUMERIC == cell.getCellType()) { |
|
733 |
format = cell.getCellStyle().getDataFormat(); |
|
734 |
if(format == 14 || format == 31 || format == 57 || format == 58){ |
|
735 |
//日期(中文时间格式的) |
|
736 |
d = cell.getDateCellValue(); |
|
737 |
formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
738 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
739 |
arrayString.add(formater.format(d)); |
|
740 |
//arrayString[i] = formater.format(d); |
|
741 |
}else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
742 |
d = cell.getDateCellValue(); |
|
743 |
//DateFormat formater = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); |
|
744 |
formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
745 |
arrayString.add(formater.format(d)); |
|
746 |
//arrayString[i] = formater.format(d); |
|
747 |
} else { |
|
748 |
if(CellType.STRING == cell.getCellType()){ |
|
749 |
arrayString.add(cell.getStringCellValue()); |
|
750 |
//arrayString[i] =cell.getStringCellValue(); |
|
751 |
}else if(CellType.FORMULA==cell.getCellType()){ |
|
752 |
arrayString.add(cell.getCellFormula()); |
|
753 |
//arrayString[i] =cell.getCellFormula(); |
|
754 |
}else if(CellType.NUMERIC== cell.getCellType()){ |
|
755 |
dataFormatter = new HSSFDataFormatter(); |
|
756 |
arrayString.add(dataFormatter.formatCellValue(cell)); |
|
757 |
//arrayString[i] =dataFormatter.formatCellValue(cell); |
|
758 |
} |
|
759 |
} |
|
760 |
} |
|
761 |
} else if(cell.getCellType() == CellType.BLANK){ |
|
762 |
arrayString.add(""); |
|
763 |
//arrayString[i] = ""; |
|
764 |
} else { // 如果EXCEL表格中的数据类型为字符串型 |
|
765 |
arrayString.add(cell.getStringCellValue().trim()); |
|
766 |
//arrayString[i] = cell.getStringCellValue().trim(); |
|
767 |
} |
|
768 |
} |
|
769 |
list.add(arrayString); |
|
770 |
} |
|
771 |
} |
|
772 |
} |
|
773 |
return list; |
|
774 |
} catch (Exception e) { |
|
775 |
e.printStackTrace(); |
|
776 |
} |
|
777 |
return null; |
|
778 |
} |
|
779 |
|
|
780 |
/**读取excel文件,兼容2003和2007 |
|
781 |
* 通过流读取Excel文件 |
|
782 |
* @return |
|
783 |
* @throws Exception |
|
784 |
*/ |
|
785 |
public static List<List<String>> getExcelDataCompatibleCheckEmpty(File file,boolean header) throws Exception { |
|
786 |
try { |
|
787 |
|
|
788 |
String fileName = file.getName(); |
|
789 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
|
790 |
throw new TipsException("上传文件格式不正确"); |
|
791 |
} |
|
792 |
|
|
793 |
// 结果集 |
|
794 |
List<List<String>> list = new ArrayList<>(); |
|
795 |
Workbook book = create(new BufferedInputStream(new FileInputStream(file))); |
|
796 |
|
|
797 |
// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 |
|
798 |
//不为空 |
|
799 |
boolean isNotEmpty = false; |
cac339
|
800 |
for(int s=0;s<book.getNumberOfSheets();s++) { |
C |
801 |
Sheet hssfsheet = book.getSheetAt(s); |
|
802 |
int col = 0; |
|
803 |
// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 去除标题 |
|
804 |
for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { |
|
805 |
Row hssfrow = hssfsheet.getRow(j); |
|
806 |
if(hssfrow!=null){ |
|
807 |
if(j == 0) { |
|
808 |
col = hssfrow.getPhysicalNumberOfCells(); |
|
809 |
if(!header) { |
|
810 |
//不包括表头 |
|
811 |
continue; |
|
812 |
} |
|
813 |
} |
|
814 |
// 单行数据 |
|
815 |
List<String> arrayString = new ArrayList<>(); |
|
816 |
for (int i = 0; i < col; i++) { |
|
817 |
Cell cell = hssfrow.getCell(i); |
|
818 |
if (cell == null) { |
|
819 |
arrayString.add(""); |
2e06d1
|
820 |
} else if (cell.getCellType() == CellType.NUMERIC) { |
cac339
|
821 |
// arrayString[i] = new Double(cell.getNumericCellValue()).toString(); |
2e06d1
|
822 |
if (CellType.NUMERIC == cell.getCellType()) { |
cac339
|
823 |
short format = cell.getCellStyle().getDataFormat(); |
C |
824 |
if(format == 14 || format == 31 || format == 57 || format == 58){ |
|
825 |
//日期(中文时间格式的) |
|
826 |
Date d = cell.getDateCellValue(); |
|
827 |
DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
828 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
829 |
arrayString.add(formater.format(d)); |
|
830 |
//arrayString[i] = formater.format(d); |
|
831 |
}else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
832 |
Date d = cell.getDateCellValue(); |
|
833 |
//DateFormat formater = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); |
|
834 |
DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
835 |
arrayString.add(formater.format(d)); |
|
836 |
//arrayString[i] = formater.format(d); |
|
837 |
} else { |
2e06d1
|
838 |
if(CellType.STRING == cell.getCellType()){ |
cac339
|
839 |
arrayString.add(cell.getStringCellValue()); |
C |
840 |
//arrayString[i] =cell.getStringCellValue(); |
2e06d1
|
841 |
}else if(CellType.FORMULA==cell.getCellType()){ |
cac339
|
842 |
arrayString.add(cell.getCellFormula()); |
C |
843 |
//arrayString[i] =cell.getCellFormula(); |
2e06d1
|
844 |
}else if(CellType.NUMERIC== cell.getCellType()){ |
cac339
|
845 |
HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); |
C |
846 |
arrayString.add(dataFormatter.formatCellValue(cell)); |
|
847 |
//arrayString[i] =dataFormatter.formatCellValue(cell); |
|
848 |
} |
|
849 |
} |
|
850 |
} |
2e06d1
|
851 |
isNotEmpty = true; |
C |
852 |
} else if(cell.getCellType() == CellType.BLANK){ |
cac339
|
853 |
arrayString.add(""); |
C |
854 |
//arrayString[i] = ""; |
|
855 |
} else { // 如果EXCEL表格中的数据类型为字符串型 |
|
856 |
arrayString.add(cell.getStringCellValue().trim()); |
|
857 |
//arrayString[i] = cell.getStringCellValue().trim(); |
2e06d1
|
858 |
isNotEmpty = true; |
cac339
|
859 |
} |
C |
860 |
} |
2e06d1
|
861 |
if (isNotEmpty){ |
C |
862 |
list.add(arrayString); |
|
863 |
} |
d57fc1
|
864 |
} |
F |
865 |
} |
|
866 |
} |
|
867 |
return list; |
|
868 |
} catch (Exception e) { |
|
869 |
e.printStackTrace(); |
|
870 |
} |
|
871 |
return null; |
|
872 |
} |
|
873 |
|
|
874 |
|
2e06d1
|
875 |
/**读取excel文件,兼容2003和2007 |
C |
876 |
* 通过流读取Excel文件 |
|
877 |
* @return |
|
878 |
* @throws Exception |
|
879 |
*/ |
|
880 |
public static List<List<String>> getExcelDataCompatibleCheckEmpty(MultipartFile file,boolean header) throws Exception { |
|
881 |
try { |
|
882 |
|
|
883 |
String fileName = file.getOriginalFilename(); |
|
884 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
|
885 |
throw new TipsException("上传文件格式不正确"); |
|
886 |
} |
|
887 |
|
|
888 |
// 结果集 |
|
889 |
List<List<String>> list = new ArrayList<>(); |
|
890 |
Workbook book = create(new BufferedInputStream(file.getInputStream())); |
|
891 |
|
|
892 |
// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 |
|
893 |
//不为空 |
|
894 |
boolean isNotEmpty = false; |
|
895 |
for(int s=0;s<book.getNumberOfSheets();s++) { |
|
896 |
Sheet hssfsheet = book.getSheetAt(s); |
|
897 |
int col = 0; |
|
898 |
// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 去除标题 |
|
899 |
for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { |
|
900 |
isNotEmpty = false; |
|
901 |
Row hssfrow = hssfsheet.getRow(j); |
|
902 |
if(hssfrow!=null){ |
|
903 |
if(j == 0) { |
|
904 |
col = hssfrow.getPhysicalNumberOfCells(); |
|
905 |
if(!header) { |
|
906 |
//不包括表头 |
|
907 |
continue; |
|
908 |
} |
|
909 |
} |
|
910 |
// 单行数据 |
|
911 |
List<String> arrayString = new ArrayList<>(); |
|
912 |
for (int i = 0; i < col; i++) { |
|
913 |
Cell cell = hssfrow.getCell(i); |
|
914 |
if (cell == null) { |
|
915 |
arrayString.add(""); |
|
916 |
} else if (cell.getCellType() == CellType.NUMERIC) { |
|
917 |
// arrayString[i] = new Double(cell.getNumericCellValue()).toString(); |
|
918 |
if (CellType.NUMERIC == cell.getCellType()) { |
|
919 |
short format = cell.getCellStyle().getDataFormat(); |
|
920 |
if(format == 14 || format == 31 || format == 57 || format == 58){ |
|
921 |
//日期(中文时间格式的) |
|
922 |
Date d = cell.getDateCellValue(); |
|
923 |
DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
924 |
// DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
925 |
arrayString.add(formater.format(d)); |
|
926 |
//arrayString[i] = formater.format(d); |
|
927 |
}else if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
928 |
Date d = cell.getDateCellValue(); |
|
929 |
//DateFormat formater = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); |
|
930 |
DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
|
931 |
arrayString.add(formater.format(d)); |
|
932 |
//arrayString[i] = formater.format(d); |
|
933 |
} else { |
|
934 |
if(CellType.STRING == cell.getCellType()){ |
|
935 |
arrayString.add(cell.getStringCellValue()); |
|
936 |
//arrayString[i] =cell.getStringCellValue(); |
|
937 |
}else if(CellType.FORMULA==cell.getCellType()){ |
|
938 |
arrayString.add(cell.getCellFormula()); |
|
939 |
//arrayString[i] =cell.getCellFormula(); |
|
940 |
}else if(CellType.NUMERIC== cell.getCellType()){ |
|
941 |
HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); |
|
942 |
arrayString.add(dataFormatter.formatCellValue(cell)); |
|
943 |
//arrayString[i] =dataFormatter.formatCellValue(cell); |
|
944 |
} |
|
945 |
} |
|
946 |
} |
|
947 |
isNotEmpty = true; |
|
948 |
} else if(cell.getCellType() == CellType.BLANK){ |
|
949 |
arrayString.add(""); |
|
950 |
//arrayString[i] = ""; |
|
951 |
} else { // 如果EXCEL表格中的数据类型为字符串型 |
|
952 |
arrayString.add(cell.getStringCellValue().trim()); |
|
953 |
isNotEmpty = true; |
|
954 |
//arrayString[i] = cell.getStringCellValue().trim(); |
|
955 |
} |
|
956 |
} |
|
957 |
if (isNotEmpty){ |
|
958 |
list.add(arrayString); |
|
959 |
} |
|
960 |
} |
|
961 |
} |
|
962 |
} |
|
963 |
return list; |
|
964 |
} catch (Exception e) { |
|
965 |
e.printStackTrace(); |
|
966 |
} |
|
967 |
return null; |
|
968 |
} |
|
969 |
|
|
970 |
|
|
971 |
/*private static String getCellVal(Cell cell) { |
cac339
|
972 |
if (null == cell) { |
C |
973 |
return ""; |
|
974 |
} |
|
975 |
switch (cell.getCellType()) { |
|
976 |
// 数字 |
2e06d1
|
977 |
case CellType.NUMERIC: |
cac339
|
978 |
// 日期格式的处理 |
C |
979 |
if (HSSFDateUtil.isCellDateFormatted(cell)) { |
|
980 |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
|
981 |
return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString(); |
|
982 |
} |
|
983 |
return String.valueOf(cell.getNumericCellValue()); |
|
984 |
// 字符串 |
2e06d1
|
985 |
case CellType.STRING: |
cac339
|
986 |
return cell.getStringCellValue(); |
C |
987 |
// 公式 |
2e06d1
|
988 |
case CellType.FORMULA: |
cac339
|
989 |
return cell.getCellFormula(); |
C |
990 |
// 空白 |
2e06d1
|
991 |
case CellType.BLANK: |
cac339
|
992 |
return ""; |
2e06d1
|
993 |
case CellType.BOOLEAN: |
cac339
|
994 |
return cell.getBooleanCellValue() + ""; |
C |
995 |
// 错误类型 |
2e06d1
|
996 |
case CellType.ERROR: |
cac339
|
997 |
return cell.getErrorCellValue() + ""; |
C |
998 |
default: |
|
999 |
break; |
|
1000 |
} |
|
1001 |
return ""; |
2e06d1
|
1002 |
}*/ |
cac339
|
1003 |
|
e7c3ff
|
1004 |
/** |
C |
1005 |
* p判断是否excel文件 |
|
1006 |
* @param file |
|
1007 |
* @return |
|
1008 |
*/ |
|
1009 |
public static boolean isExcel(MultipartFile file){ |
|
1010 |
String fileName = file.getOriginalFilename(); |
|
1011 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
|
1012 |
return false; |
|
1013 |
} |
|
1014 |
return true; |
|
1015 |
} |
|
1016 |
|
|
1017 |
|
6b6bf5
|
1018 |
|
W |
1019 |
|
|
1020 |
|
|
1021 |
public static File createExcelByImg(String[] headList, String[] fieldList, List<Map<String, Object>> dataList, Integer height, Integer width) throws Exception { |
|
1022 |
File file = File.createTempFile("temp", ".xls"); |
|
1023 |
FileOutputStream fileOut = null; |
|
1024 |
BufferedImage bufferImg = null; |
|
1025 |
|
|
1026 |
try { |
|
1027 |
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); |
|
1028 |
if (height == null) { |
|
1029 |
height = 450; |
|
1030 |
} |
|
1031 |
|
|
1032 |
if (width == null) { |
|
1033 |
width = 1000; |
|
1034 |
} |
|
1035 |
|
|
1036 |
HSSFWorkbook workbook = new HSSFWorkbook(); |
|
1037 |
HSSFCellStyle boderStyle = workbook.createCellStyle(); |
2e06d1
|
1038 |
boderStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
C |
1039 |
boderStyle.setAlignment(HorizontalAlignment.CENTER); |
6b6bf5
|
1040 |
HSSFSheet sheet = workbook.createSheet(); |
W |
1041 |
HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); |
|
1042 |
HSSFRow row = sheet.createRow(0); |
|
1043 |
|
|
1044 |
HSSFCell anchor; |
|
1045 |
for(int i = 0; i < headList.length; ++i) { |
|
1046 |
row.setHeight(height.shortValue()); |
|
1047 |
sheet.setColumnWidth(i, width); |
|
1048 |
anchor = row.createCell(i); |
|
1049 |
anchor.setCellValue(headList[i]); |
|
1050 |
anchor.setCellStyle(boderStyle); |
|
1051 |
} |
|
1052 |
|
|
1053 |
HSSFRow row_value = null; |
|
1054 |
anchor = null; |
|
1055 |
HSSFCell cell = null; |
|
1056 |
if (dataList != null) { |
|
1057 |
for(int n = 0; n < dataList.size(); ++n) { |
|
1058 |
row_value = sheet.createRow(n + 1); |
|
1059 |
row_value.setHeight(height.shortValue()); |
|
1060 |
Map<String, Object> dataMap = (Map)dataList.get(n); |
|
1061 |
|
|
1062 |
for(int i = 0; i < fieldList.length; ++i) { |
|
1063 |
sheet.setColumnWidth(i, width); |
|
1064 |
cell = row_value.createCell(i); |
|
1065 |
Object value = dataMap.get(fieldList[i]); |
|
1066 |
if (value != null && "class java.io.File".equals(value.getClass().toString())) { |
|
1067 |
File file2 = (File)value; |
|
1068 |
if (file2 == null) { |
|
1069 |
cell.setCellValue(""); |
|
1070 |
} else { |
|
1071 |
bufferImg = ImageIO.read(file2); |
|
1072 |
ImageIO.write(bufferImg, "jpg", byteArrayOut); |
|
1073 |
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 1023, 255, (short)i, n + 1, (short)i, n + 1); |
2e06d1
|
1074 |
anchor1.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); |
6b6bf5
|
1075 |
patriarch.createPicture(anchor1, workbook.addPicture(byteArrayOut.toByteArray(), 5)); |
W |
1076 |
} |
|
1077 |
} else { |
|
1078 |
cell.setCellValue(objToString(dataMap.get(fieldList[i]))); |
|
1079 |
cell.setCellStyle(boderStyle); |
|
1080 |
} |
|
1081 |
} |
|
1082 |
} |
|
1083 |
} |
|
1084 |
|
|
1085 |
FileOutputStream fOut = new FileOutputStream(file); |
|
1086 |
workbook.write(fOut); |
|
1087 |
fOut.flush(); |
|
1088 |
fOut.close(); |
|
1089 |
} catch (Exception var25) { |
|
1090 |
var25.printStackTrace(); |
|
1091 |
} finally { |
|
1092 |
file.deleteOnExit(); |
|
1093 |
} |
|
1094 |
|
|
1095 |
return file; |
|
1096 |
} |
2e06d1
|
1097 |
public static void main(String[] args) throws Exception { |
C |
1098 |
//File file = new File("E:\\360\\a701aae8-9d4b-49f1-8999-1456fa53ef03 (1).xlsx"); |
|
1099 |
File file = new File("E:\\360\\tttt (1).xls"); |
|
1100 |
//Workbook wk = create(new FileInputStream(file)); |
|
1101 |
Workbook wk = StreamingReader.builder() |
|
1102 |
.rowCacheSize(100) // 缓存到内存中的行数(默认是10) |
|
1103 |
.bufferSize(4096) // 读取资源时,缓存到内存的字节大小(默认是1024) |
|
1104 |
.open(new FileInputStream(file)); // InputStream或者XLSX格式的File(必须) |
|
1105 |
|
|
1106 |
Sheet sheet = wk.getSheetAt(0); |
|
1107 |
//遍历所有的行 |
|
1108 |
for (Row row : sheet) { |
|
1109 |
System.out.println("开始遍历第" + row.getRowNum() + "行数据:"); |
|
1110 |
//遍历所有的列 |
|
1111 |
for (Cell cell : row) { |
|
1112 |
System.out.print(cell.getStringCellValue() + " "); |
|
1113 |
} |
|
1114 |
System.out.println(" "); |
|
1115 |
} |
|
1116 |
} |
|
1117 |
|
cac339
|
1118 |
} |