Maven依赖
<dependencies>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!--xlsx-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
读取操作
//url:文件地址
//sheet:sheet页号,从0开始
public static List<List<String>> readExcel(String url,int sheet) throws Exception {
FileInputStream fis=new FileInputStream(url);
// Workbook workbook=new HSSFWorkbook(fis); xls
Workbook workbook = new XSSFWorkbook(fis);
//读取第一个sheet
Sheet sheet = workbook.getSheetAt(sheet);
// 获取行
Iterator<Row> rows = sheet.rowIterator();
Row row;
Cell cell;
List<List<String>> list = new ArrayList<>();
while (rows.hasNext()) {
row = rows.next();
// 获取单元格
Iterator<Cell> cells = row.cellIterator();
int i = 0;
List<String> rowLis = new ArrayList();
while (cells.hasNext()) {
cell = cells.next();
String cellValue = cell.getStringCellValue();
// System.out.print(cellValue + "");
rowLis.add(cellValue);
i++;
}
list.add(rowLis);
}
// System.out.println(list);
return list;
}
合并单元格可以先拆解,获取数据后再进行处理