ExcelReadUtil.java
1.32 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
package com.cjs.cms.util.poi;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
/**
* 读取Excel内容
*
* @author tongyufu
*
*/
public class ExcelReadUtil {
private ExcelReadConfig config;
public ExcelReadUtil(ExcelReadConfig config) {
this.config = config;
}
/**
* 读取Excel内容
* @param input
* @throws Exception
*/
public List<String[]> read() throws Exception {
List<String[]> dataList = new ArrayList<String[]>();
Workbook wb = WorkbookFactory.create(config.getSourceFile());
Sheet sheet = wb.getSheetAt(0);
int rowCount = sheet.getLastRowNum() + 1;
Row row = null;
for (int i = config.getStartRow(); i < rowCount; i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
}
short cellCount = row.getLastCellNum();
String[] rowValue = new String[cellCount];
for (short j = 0; j < cellCount; j++) {
rowValue[j] = ExcelUtil.getCellValue(row.getCell(j));
}
dataList.add(rowValue);
}
return dataList;
}
}