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.time.LocalDate; import java.time.ZoneId; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; @Slf4j public class ExcelUtil { public static List> getExcelRowList(Workbook workbook) throws IOException { //行List,也是最终要返回的List List> rowList=new ArrayList<>(); 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)) { Date date = cell.getDateCellValue(); if (HSSFDateUtil.isValidExcelDate(date.getTime())) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String value = sdf.format(date); map.put(cellNum,value); } else { map.put(cellNum,""); } continue; } if(cell != null){ DataFormatter formatter = new DataFormatter(); String phoneNumber = formatter.formatCellValue(cell); String value = fommartNum(phoneNumber.trim()); map.put(cellNum,value); } } rowList.add(map); } return rowList; } public static List> getExcelRowList(MultipartFile multipartFile) throws IOException { return getExcelRowList(getExcelWorkBook(multipartFile)); } public static List> getExcelRowList(InputStream inputStream) throws IOException { return getExcelRowList(getExcelWorkBook(inputStream)); } private static String fommartNum(String value){ try { if(isNumeric2(value) && value.contains(".")){ 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 LocalDate getCellLocalDate(Cell cell) { if (cell == null || cell.getCellType() == CellType.BLANK) { return null; } if (cell.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); if (DateUtil.getExcelDate(date) == 0) { return null; } return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); } return null; } private static Workbook getExcelWorkBook(InputStream inputStream) throws IOException { return new XSSFWorkbook(inputStream); } 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); } } }