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