package com.fdkankan.agent.util; import lombok.extern.slf4j.Slf4j; import org.apache.poi.hssf.usermodel.HSSFDateUtil; 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.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; @Slf4j public class ExcelUtil { public static List> getExcelRowList(MultipartFile multipartFile) throws IOException { //行List,也是最终要返回的List List> rowList=new ArrayList<>(); Workbook workbook=getExcelWorkBook(multipartFile); Sheet sheet = workbook.getSheetAt(0); if (sheet == null) { throw new IOException("创建Sheet失败!"); } //开始遍历行 for (int i=0;i<= sheet.getLastRowNum();i++){ Row row = sheet.getRow(i); //列List HashMap map = new HashMap<>(); if(row == null){ rowList.add(map); continue; } //转换为List数组 for (int cellNum=0;cellNum<= row.getLastCellNum();cellNum++){ Cell cell = row.getCell(cellNum); if (cell != null && cell.getCellTypeEnum() != CellType.STRING && HSSFDateUtil.isCellDateFormatted(cell)) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()); String value = sdf.format(date); map.put(cellNum,value); continue; } if(cell != null){ String value = fommartNum(cell.toString().trim()); map.put(cellNum,value); } } rowList.add(map); } return rowList; } private static Workbook getExcelWorkBook(InputStream inputStream) throws IOException { return new XSSFWorkbook(inputStream); } private static String fommartNum(String value){ try { if(isNumeric2(value) && value.contains(".")){ log.info("fommartNum:{}",value); return Double.valueOf(value).intValue() +""; } }catch (Exception e){ return value; } return value; } public static boolean isNumeric2(String str) { return str != null && str.matches("-?\\d+(\\.\\d+)?"); } public static List> getExcelRowList(InputStream inputStream) throws IOException { //行List,也是最终要返回的List List> rowList=new ArrayList<>(); Workbook workbook=getExcelWorkBook(inputStream); Sheet sheet = workbook.getSheetAt(0); if (sheet == null) { throw new IOException("创建Sheet失败!"); } //开始遍历行 for (int i=0;i<= sheet.getLastRowNum();i++){ Row row = sheet.getRow(i); //列List HashMap map = new HashMap<>(); if(row == null){ rowList.add(map); continue; } //转换为List数组 for (int cellNum=0;cellNum<= row.getLastCellNum();cellNum++){ Cell cell = row.getCell(cellNum); if (cell != null && cell.getCellTypeEnum() != CellType.STRING && HSSFDateUtil.isCellDateFormatted(cell)) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()); String value = sdf.format(date); map.put(cellNum,value); continue; } if(cell != null){ String stringCellValue = cell.getStringCellValue(); log.info(stringCellValue); String value = fommartNum(cell.toString().trim()); map.put(cellNum,value); } } rowList.add(map); } return rowList; } private static Workbook getExcelWorkBook(MultipartFile multipartFile) throws IOException { InputStream inputStream=multipartFile.getInputStream(); String originalFileName=multipartFile.getOriginalFilename(); assert originalFileName != null; String fileType=originalFileName.substring(originalFileName.lastIndexOf(".")+1); if (fileType.equalsIgnoreCase("xls")) { //xls格式 return new HSSFWorkbook(inputStream); } else { //xlsx格式 return new XSSFWorkbook(inputStream); } } }