导入功能代码
//导入功能
@ResponseBody
@PostMapping("/importFile")
public RespBean importFile(MultipartFile multipartFile) throws Exception{
Map map = fileUploadService.uploadExcle(multipartFile);
if(map!=null&&map.get("url")!=null){
String url=fileConfig.getPath()+map.get("url");
File file=new File(url);
// 流读取文件
FileInputStream is = new FileInputStream(file);//new File("E:/123.xls"));
// 根据流创建文件
Workbook wb = new HSSFWorkbook(is);
// 获取sheet
Sheet sheet = wb.getSheetAt(0);
//插入中间表
for(Row row:sheet){
// 第一行是标题,不需要读取
if(row.getRowNum()==0){
continue;
}
// 当遇到空行,跳过
if(row.getCell(0)==null ){//|| StringUtils.isBlank(row.getCell(1).getStringCellValue())){
continue;
}
if (row.getCell(1)==null){
continue;
}
//循环每行的信息
for (int j=0;j<=100;j++){
System.out.println(getCellValueBasic(row.getCell(j)));
}
}
}
return RespBean.success();
}
getCellValueBasic方法用来转换不同单元格的excle信息
// 获取单元格的值
public String getCellValueBasic(Cell cell){
System.out.println();
if(cell == null) return "";
if(cell.getCellType() == Cell.CELL_TYPE_STRING){
return cell.getStringCellValue();
}else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){
return String.valueOf(cell.getBooleanCellValue());
}else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
return cell.getCellFormula() ;
}else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
return String.valueOf(cell.getNumericCellValue());
}
return "";
}