信息提取规则:1.excel第一行为标题,不提取;2.标题为空的那列数据不提取
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
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;
public List<List<String>> analyzeFile(MultipartFile file) {
List<List<String>> list = new ArrayList<>();
try {
Workbook work = null;
String fileName = file.getOriginalFilename();
InputStream in = file.getInputStream();
String filetype = fileName.substring(fileName.lastIndexOf("."));
if(".xls".equals(filetype)) {
work = new HSSFWorkbook(in);
} else if (".xlsx".equals(filetype)) {
work = new XSSFWorkbook(in);
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if(sheet == null) {
continue;
}
//获取标题行所在的列
List<Integer> colNums = new ArrayList<>();
row = sheet.getRow(sheet.getFirstRowNum());
for (int a=row.getFirstCellNum();a<=row.getLastCellNum();a++){
cell = row.getCell(a);
if (cell==null){
continue;
}
cell.setCellType(CellType.STRING);
if ("".equals(cell.getStringCellValue().trim())){
continue;
}
colNums.add(a);
}
// 滤过第一行标题
for (int j = sheet.getFirstRowNum()+1; j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
if (row == null) {
continue;
}
List<String> li = new ArrayList<>();
for (int y:colNums){
cell = row.getCell(y);
if (cell == null){
li.add(null);
continue;
}
cell.setCellType(CellType.STRING);
if ("".equals(cell.getStringCellValue().trim())){
li.add(null);
continue;
}
li.add(cell.getStringCellValue().trim());
}
list.add(li);
}
}
work.close();
}catch (Exception e){
}
return list;
}