|
|
1
|
+package com.tianbo.util.ExcelUtils;
|
|
|
2
|
+
|
|
|
3
|
+
|
|
|
4
|
+
|
|
|
5
|
+import org.apache.commons.lang.StringUtils;
|
|
|
6
|
+import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
7
|
+import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
8
|
+import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
9
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
10
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
11
|
+import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
12
|
+import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
13
|
+import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
14
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
15
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
16
|
+
|
|
|
17
|
+import java.io.InputStream;
|
|
|
18
|
+import java.util.ArrayList;
|
|
|
19
|
+import java.util.List;
|
|
|
20
|
+
|
|
|
21
|
+/**
|
|
|
22
|
+ * shenhailong
|
|
|
23
|
+ * 2020/4/27/14:29
|
|
|
24
|
+ */
|
|
|
25
|
+public class ExcelUtils {
|
|
|
26
|
+
|
|
|
27
|
+
|
|
|
28
|
+
|
|
|
29
|
+ /**
|
|
|
30
|
+ * 读取.xlsx 内容
|
|
|
31
|
+ * @param file
|
|
|
32
|
+ * @return
|
|
|
33
|
+ * @throws
|
|
|
34
|
+ */
|
|
|
35
|
+ public static List<ArrayList<String>> readXlsx (MultipartFile file) {
|
|
|
36
|
+ List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
|
|
|
37
|
+ InputStream input = null;
|
|
|
38
|
+ XSSFWorkbook wb = null;
|
|
|
39
|
+ try {
|
|
|
40
|
+ input = file.getInputStream();
|
|
|
41
|
+ //创建文档
|
|
|
42
|
+ wb = new XSSFWorkbook(input);
|
|
|
43
|
+ ArrayList<String> rowList = null;
|
|
|
44
|
+ int totoalRows = 0;//总行数
|
|
|
45
|
+ int totalCells = 0;//总列数
|
|
|
46
|
+ //读取sheet(页)
|
|
|
47
|
+ for (int sheetIndex = 0 ; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
|
|
|
48
|
+ XSSFSheet xssfSheet = wb.getSheetAt(sheetIndex);
|
|
|
49
|
+
|
|
|
50
|
+ if (xssfSheet == null) {
|
|
|
51
|
+ continue;
|
|
|
52
|
+ }
|
|
|
53
|
+ totoalRows = xssfSheet.getLastRowNum();
|
|
|
54
|
+ //读取row
|
|
|
55
|
+ for (int rowIndex = 2; rowIndex <= totoalRows; rowIndex++) {
|
|
|
56
|
+ XSSFRow xssfRow = xssfSheet.getRow(rowIndex);
|
|
|
57
|
+
|
|
|
58
|
+ if (xssfRow == null) {
|
|
|
59
|
+ continue;
|
|
|
60
|
+ }
|
|
|
61
|
+ rowList = new ArrayList<String>();
|
|
|
62
|
+ totalCells = xssfRow.getLastCellNum();
|
|
|
63
|
+
|
|
|
64
|
+ //读取列
|
|
|
65
|
+ for (int cellIndex = 0; cellIndex < totalCells; cellIndex++) {
|
|
|
66
|
+ XSSFCell xssfCell = xssfRow.getCell(cellIndex);
|
|
|
67
|
+ if (xssfCell == null) {
|
|
|
68
|
+ rowList.add("");
|
|
|
69
|
+ } else {
|
|
|
70
|
+// xssfCell.setCellType(Cell.CELL_TYPE_STRING);
|
|
|
71
|
+ rowList.add(String.valueOf(xssfCell.getStringCellValue()));
|
|
|
72
|
+ }
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+ list.add(rowList);
|
|
|
76
|
+
|
|
|
77
|
+ }
|
|
|
78
|
+ }
|
|
|
79
|
+ } catch (Exception e) {
|
|
|
80
|
+ e.printStackTrace();
|
|
|
81
|
+ return null;
|
|
|
82
|
+ } finally {
|
|
|
83
|
+ try {
|
|
|
84
|
+ if ( wb != null) {
|
|
|
85
|
+ wb.close();
|
|
|
86
|
+ }
|
|
|
87
|
+ if (input != null) {
|
|
|
88
|
+ input.close();
|
|
|
89
|
+ }
|
|
|
90
|
+ } catch (Exception e) {
|
|
|
91
|
+ }
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+ return list;
|
|
|
95
|
+ }
|
|
|
96
|
+
|
|
|
97
|
+
|
|
|
98
|
+
|
|
|
99
|
+ /**
|
|
|
100
|
+ * 读取 .xls内容
|
|
|
101
|
+ * @param file
|
|
|
102
|
+ * @return
|
|
|
103
|
+ * @throws
|
|
|
104
|
+ */
|
|
|
105
|
+ public static List<ArrayList<String>> readXls (MultipartFile file) {
|
|
|
106
|
+ List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
|
|
|
107
|
+
|
|
|
108
|
+ //创建输入流
|
|
|
109
|
+ InputStream input = null;
|
|
|
110
|
+ //创建文档
|
|
|
111
|
+ HSSFWorkbook wb = null;
|
|
|
112
|
+
|
|
|
113
|
+ try {
|
|
|
114
|
+ input = file.getInputStream();
|
|
|
115
|
+ //创建文档
|
|
|
116
|
+ wb = new HSSFWorkbook(input);
|
|
|
117
|
+
|
|
|
118
|
+ ArrayList<String> rowList = null;
|
|
|
119
|
+ int totoalRows = 0;//总行数
|
|
|
120
|
+ int totalCells = 0;//总列数
|
|
|
121
|
+ //读取sheet(页)
|
|
|
122
|
+ for (int sheetIndex = 0 ; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
|
|
|
123
|
+ HSSFSheet hssfSheet = wb.getSheetAt(sheetIndex);
|
|
|
124
|
+
|
|
|
125
|
+ if (hssfSheet == null) {
|
|
|
126
|
+ continue;
|
|
|
127
|
+ }
|
|
|
128
|
+
|
|
|
129
|
+ totoalRows = hssfSheet.getLastRowNum();
|
|
|
130
|
+ //读取row
|
|
|
131
|
+ for (int rowIndex = 2; rowIndex <= totoalRows; rowIndex++) {
|
|
|
132
|
+ HSSFRow hssfRow = hssfSheet.getRow(rowIndex);
|
|
|
133
|
+
|
|
|
134
|
+ if (hssfRow == null) {
|
|
|
135
|
+ continue;
|
|
|
136
|
+ }
|
|
|
137
|
+ rowList = new ArrayList<String>();
|
|
|
138
|
+ totalCells = hssfRow.getLastCellNum();
|
|
|
139
|
+
|
|
|
140
|
+ //读取列
|
|
|
141
|
+ for (int cellIndex = 0; cellIndex < totalCells; cellIndex++) {
|
|
|
142
|
+ HSSFCell hssfCell = hssfRow.getCell(cellIndex);
|
|
|
143
|
+ if (hssfCell == null) {
|
|
|
144
|
+ rowList.add("");
|
|
|
145
|
+ } else {
|
|
|
146
|
+ hssfCell.setCellType(Cell.CELL_TYPE_STRING);
|
|
|
147
|
+ rowList.add(String.valueOf(hssfCell.getStringCellValue()));
|
|
|
148
|
+ }
|
|
|
149
|
+ }
|
|
|
150
|
+
|
|
|
151
|
+ list.add(rowList);
|
|
|
152
|
+
|
|
|
153
|
+ }
|
|
|
154
|
+ }
|
|
|
155
|
+ } catch (Exception e) {
|
|
|
156
|
+ e.printStackTrace();
|
|
|
157
|
+ return null;
|
|
|
158
|
+ } finally {
|
|
|
159
|
+ try {
|
|
|
160
|
+ if ( wb != null) {
|
|
|
161
|
+// wb.close();
|
|
|
162
|
+ }
|
|
|
163
|
+ if (input != null) {
|
|
|
164
|
+ input.close();
|
|
|
165
|
+ }
|
|
|
166
|
+ } catch (Exception e) {
|
|
|
167
|
+ }
|
|
|
168
|
+ }
|
|
|
169
|
+ return list;
|
|
|
170
|
+ }
|
|
|
171
|
+
|
|
|
172
|
+
|
|
|
173
|
+ public static String getPostfix (String path) {
|
|
|
174
|
+ if (StringUtils.isBlank(path) || !path.contains(".")) {
|
|
|
175
|
+ return null;
|
|
|
176
|
+ }
|
|
|
177
|
+ return path.substring(path.lastIndexOf(".") + 1, path.length()).trim();
|
|
|
178
|
+ }
|
|
|
179
|
+
|
|
|
180
|
+} |