package org.jeecg.modules.cloud.controller; import cn.hutool.core.util.ArrayUtil; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); List<String[]> arrayList = new ArrayList<>(); try { FileInputStream fis = new FileInputStream("D:\\Focus Hospital校验验证数据.xlsx"); Workbook wb = new XSSFWorkbook(fis); Sheet sheet = wb.getSheetAt(0); for (Row row : sheet) { String[] strArray = new String[9]; for (int i = 0; i < 9; i++) { Cell cell = row.getCell(i); FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); if (cell != null) { switch (cell.getCellType().name()) { case "STRING": strArray[i] = cell.getRichStringCellValue().getString(); break; case "NUMERIC": if("General".equals(cell.getCellStyle().getDataFormatString())){ cell.setCellType(CellType.STRING); strArray[i] = row.getCell(i).getStringCellValue(); }else { // 此方法用来判断是否为日期 // boolean cellDateFormatted = HSSFDateUtil.isCellDateFormatted(cell); // if(cellDateFormatted){ strArray[i] = cell.getDateCellValue()+""; // strArray[i] = cell.getRichStringCellValue()+""; // } // strArray[i] = String.valueOf(cell.getNumericCellValue()); DataFormatter formatter = new DataFormatter(); formatter.getDefaultFormat(cell); strArray[i] = formatter.formatCellValue(cell, evaluator); } break; case "BOOLEAN": strArray[i] = String.valueOf(cell.getBooleanCellValue()); break; case "BLANK": strArray[i] = ""; break; default: strArray[i] = cell.toString(); break; } }else{ strArray[i]=""; } } /**排除空行*/ if(ArrayUtil.isNotEmpty(strArray)){ arrayList.add(strArray); } } System.out.println(arrayList.size()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
poi 判断几种单元格类型
最新推荐文章于 2023-10-18 23:38:44 发布