public class AnalysisExcelDataUtil { public static List<List> getExcelData(MultipartFile file, boolean readFirstRow) throws IOException { checkFile(file); Workbook workbook = getWorkBook(file); List<List> list = new ArrayList<>(); if (workbook != null) { for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) { Sheet sheet = workbook.getSheetAt(sheetNum); if (sheet == null) { continue; } int firstRowNum = sheet.getFirstRowNum(); int lastRowNum = sheet.getLastRowNum(); for (int rowNum = (readFirstRow ? firstRowNum : firstRowNum + 1); rowNum <= lastRowNum; rowNum++) { Row row = sheet.getRow(rowNum); if (row == null) { continue; } int firstCellNum = row.getFirstCellNum(); int lastCellNum = row.getLastCellNum(); if (lastCellNum > 0) { ArrayList<Object> cellValues = new ArrayList<>(); for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {Cell cell = row.getCell(cellNum);cellValues.add(getCellValue(cell)); } list.add(cellValues); } } } } return list; } public static void checkFile(MultipartFile file) throws IOException { if (null == file) { System.err.println("文件不存在!"); } String fileName = file.getOriginalFilename(); if (!fileName.endsWith("xls") && !fileName.endsWith("xlsx")) { System.err.println("不是excel文件"); } } public static Workbook getWorkBook(MultipartFile file) { String fileName = file.getOriginalFilename(); Workbook workbook = null; try { InputStream is = file.getInputStream(); if (fileName.endsWith("xls")) { workbook = new HSSFWorkbook(is); } else if (fileName.endsWith("xlsx")) { workbook = new XSSFWorkbook(is); } } catch (IOException e) { e.getMessage(); } return workbook; } public static String getCellValue(Cell cell) { String cellValue = ""; if (cell == null) { return cellValue; } switch (cell.getCellTypeEnum()) { case NUMERIC: cellValue = stringDateProcess(cell); break; case STRING: cellValue = String.valueOf(cell.getStringCellValue()); break; case BOOLEAN: cellValue = String.valueOf(cell.getBooleanCellValue()); break; case FORMULA: cellValue = String.valueOf(cell.getCellFormula()); break; case BLANK: cellValue = ""; break; case ERROR: cellValue = "非法字符"; break; default: cellValue = "未知类型"; break; } return cellValue; } public static String stringDateProcess(Cell cell) { String result = new String(); if (HSSFDateUtil.isCellDateFormatted(cell)) { SimpleDateFormat sdf = null; if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) { sdf = new SimpleDateFormat("HH:mm"); } else { sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); } Date date = cell.getDateCellValue(); result = sdf.format(date); } else if (cell.getCellStyle().getDataFormat() == 58) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); double value = cell.getNumericCellValue(); Date date = org.apache.poi.ss.usermodel.DateUtil .getJavaDate(value); result = sdf.format(date); } else { double value = cell.getNumericCellValue(); CellStyle style = cell.getCellStyle(); DecimalFormat format = new DecimalFormat(); String temp = style.getDataFormatString(); if (temp.equals("General")) { format.applyPattern("#"); } result = format.format(value); } return result; } public static InputStream convertorStream(Workbook workbook) { InputStream in = null; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); workbook.write(out); byte[] bookByteAry = out.toByteArray(); in = new ByteArrayInputStream(bookByteAry); } catch (Exception e) { e.printStackTrace(); } return in; } public static String cellDataToString(Object obj) { if (obj == null) { return ""; } String objString = String.valueOf(obj); if (objString == null) { return ""; } return objString.trim(); }}