提交 | 用户 | age
|
826b66
|
1 |
package com.hx.util; |
C |
2 |
|
|
3 |
|
|
4 |
import com.hx.exception.TipsException; |
|
5 |
import jxl.CellType; |
|
6 |
import org.apache.poi.hssf.usermodel.*; |
|
7 |
import org.apache.poi.ss.usermodel.*; |
|
8 |
import org.springframework.web.multipart.MultipartFile; |
|
9 |
|
|
10 |
import java.io.File; |
|
11 |
import java.io.FileInputStream; |
|
12 |
import java.io.FileOutputStream; |
|
13 |
import java.util.ArrayList; |
|
14 |
import java.util.Date; |
|
15 |
import java.util.List; |
|
16 |
import java.util.Map; |
|
17 |
|
|
18 |
/** |
|
19 |
* Excel工具类(兼容poi4.0+) |
|
20 |
*/ |
|
21 |
public class ExcelFileUtil { |
|
22 |
|
|
23 |
/** |
|
24 |
* 读取 Excel文件内容 |
|
25 |
* |
|
26 |
* @param file |
|
27 |
* @param header 是否包括表头 |
|
28 |
* @return |
|
29 |
* @throws Exception |
|
30 |
*/ |
|
31 |
public static List<List<String>> readExcelByeFileData(MultipartFile file, boolean header) throws Exception { |
|
32 |
// 结果集 |
|
33 |
List<List<String>> list = new ArrayList<>(); |
|
34 |
if (file == null){ |
|
35 |
return list; |
|
36 |
} |
|
37 |
String fileName = file.getOriginalFilename(); |
|
38 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
|
39 |
throw new TipsException("上传文件格式不正确"); |
|
40 |
} |
|
41 |
|
|
42 |
Workbook hssfworkbook = WorkbookFactory.create(file.getInputStream()); |
|
43 |
|
|
44 |
int num = hssfworkbook.getNumberOfSheets(); |
|
45 |
if (num == 0){ |
|
46 |
return list; |
|
47 |
} |
|
48 |
List<String> rowList; |
|
49 |
for (int k = 0; k< num ; k++) { |
|
50 |
Sheet sheet = hssfworkbook.getSheetAt(k); |
|
51 |
|
|
52 |
for (int i = 0; i <= sheet.getLastRowNum(); i++) { |
|
53 |
rowList = new ArrayList<>(); |
|
54 |
// 获取第i行的数据 |
|
55 |
Row row = sheet.getRow(i); |
|
56 |
// getLastCellNum() 获取这一行中单元格的数量 |
|
57 |
for (int j = 0; j < row.getLastCellNum(); j++) { |
|
58 |
// 获取第i行第j列的单元格数据 |
|
59 |
rowList.add(row.getCell(j).toString()); |
|
60 |
} |
|
61 |
list.add(rowList); |
|
62 |
} |
|
63 |
} |
|
64 |
return list; |
|
65 |
} |
|
66 |
|
|
67 |
|
|
68 |
/** |
|
69 |
* 读取 Excel文件内容 |
|
70 |
* |
|
71 |
* @param file |
|
72 |
* @param header 是否包括表头 |
|
73 |
* @return |
|
74 |
* @throws Exception |
|
75 |
*/ |
|
76 |
public static List<List<String>> readExcelByeFileData(File file, boolean header) throws Exception { |
|
77 |
// 结果集 |
|
78 |
List<List<String>> list = new ArrayList<>(); |
|
79 |
if (file == null){ |
|
80 |
return list; |
|
81 |
} |
|
82 |
String fileName = file.getName(); |
|
83 |
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
|
84 |
throw new TipsException("上传文件格式不正确"); |
|
85 |
} |
|
86 |
|
|
87 |
Workbook hssfworkbook = WorkbookFactory.create(new FileInputStream(file)); |
|
88 |
|
|
89 |
int num = hssfworkbook.getNumberOfSheets(); |
|
90 |
if (num == 0){ |
|
91 |
return list; |
|
92 |
} |
|
93 |
List<String> rowList; |
|
94 |
for (int k = 0; k< num ; k++) { |
|
95 |
Sheet sheet = hssfworkbook.getSheetAt(k); |
|
96 |
|
|
97 |
for (int i = 0; i <= sheet.getLastRowNum(); i++) { |
|
98 |
rowList = new ArrayList<>(); |
|
99 |
// 获取第i行的数据 |
|
100 |
Row row = sheet.getRow(i); |
|
101 |
// getLastCellNum() 获取这一行中单元格的数量 |
|
102 |
for (int j = 0; j < row.getLastCellNum(); j++) { |
|
103 |
// 获取第i行第j列的单元格数据 |
|
104 |
rowList.add(row.getCell(j).toString()); |
|
105 |
} |
|
106 |
list.add(rowList); |
|
107 |
} |
|
108 |
} |
|
109 |
return list; |
|
110 |
} |
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
private static String objToString(Object obj) { |
|
115 |
if (obj == null) { |
|
116 |
return ""; |
|
117 |
} else { |
|
118 |
if (obj instanceof String) { |
|
119 |
return (String) obj; |
|
120 |
} else if (obj instanceof Date) { |
|
121 |
return null; |
|
122 |
} else { |
|
123 |
return obj.toString(); |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
|
|
129 |
} |