导入excel返回List<List>
pom
<!--处理2003 excel&ndash-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<!--处理2007 excel&ndash-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
/**
* @Description: excle转成list,指定表头几行
* @Param:
* @return: List<List<String>>
* @Author: zzy
* @Date: 2019/10/15
*/
public static List<List<String>> getExcel(MultipartFile file,Integer tablehead){
List<List<String>> list=new ArrayList<>();
Workbook workbook=null;
try {
//得到Excel工作簿对象
if(file.getOriginalFilename().endsWith("xlsx")){
workbook=new XSSFWorkbook(file.getInputStream());
}else {
workbook = new HSSFWorkbook(file.getInputStream());
}
//得到Excel工作表对象
Sheet sheet = workbook.getSheetAt(0);
//得到Excel工作表的行
for (int i=tablehead;i<=sheet.getLastRowNum();i++){
Row row = sheet.getRow(i);
//第一列没有数据则跳过此行
if(row.getCell(0)==null||!StringUtils.hasText(row.getCell(0).toString())){
continue;
}
List<String> cells=new ArrayList<>();
//得到Excel工作表指定行的单元格
for (int j = 0; j <= row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
if (cell != null) {
try {
cells.add(cell.getStringCellValue());
} catch (IllegalStateException e) {
long t = (long) cell.getNumericCellValue();
cells.add(t + "");
}
} else {
cells.add("");
}
}
list.add(cells);
}
return list;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}