package com.ruoyi.common.utils; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.util.*; /** * Excel解析工具类 * 提供静态方法将Excel数据解析为List结构 * 第一列作为Map的key,其他列作为value */ public class ExcelParserUtil { /** * 解析Excel文件数据 * @param file Excel文件对象 * @return List结构的数据,第一列作为Map的key * @throws Exception 如果文件格式不支持或解析过程中出现错误 */ public static List> parseExcel(File file) throws Exception { List> resultList = new ArrayList<>(); FileInputStream fis = new FileInputStream(file); Workbook workbook; // 根据文件扩展名创建不同的Workbook实例 String fileName = file.getName().toLowerCase(); if (fileName.endsWith(".xlsx")) { workbook = new XSSFWorkbook(fis); } else if (fileName.endsWith(".xls")) { workbook = new HSSFWorkbook(fis); } else { fis.close(); throw new IllegalArgumentException("不支持的文件格式,仅支持.xls和.xlsx格式"); } try { // 获取第一个工作表 Sheet sheet = workbook.getSheetAt(0); // 获取第一行作为字段名 Row headerRow = sheet.getRow(0); if (headerRow == null) { throw new RuntimeException("Excel文件没有数据"); } // 读取字段名(第一列) String keyName = getCellValue(headerRow.getCell(0)); if (keyName == null || keyName.trim().isEmpty()) { throw new RuntimeException("第一列第一行必须包含字段名"); } // 读取数据行 for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row == null) continue; // 获取key值(第一列的值) Cell keyCell = row.getCell(0); if (keyCell == null) continue; String keyValue = getCellValue(keyCell); if (keyValue == null || keyValue.trim().isEmpty()) continue; // 创建Map存储这一行的数据 Map rowMap = new LinkedHashMap<>(); rowMap.put(keyName, keyValue); // 读取其他列的数据 for (int j = 1; j < headerRow.getLastCellNum(); j++) { Cell headerCell = headerRow.getCell(j); if (headerCell == null) continue; String columnName = getCellValue(headerCell); if (columnName == null || columnName.trim().isEmpty()) continue; Cell dataCell = row.getCell(j); String cellValue = dataCell != null ? getCellValue(dataCell) : ""; rowMap.put(columnName, cellValue); } resultList.add(rowMap); } } finally { // 确保资源被关闭 workbook.close(); fis.close(); } return resultList; } /** * 获取单元格的值 * @param cell 单元格对象 * @return 单元格的字符串表示 */ private static String getCellValue(Cell cell) { if (cell == null) { return ""; } switch (cell.getCellType()) { case STRING: return cell.getStringCellValue().trim(); case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { return cell.getDateCellValue().toString(); } else { // 避免显示为科学计数法 double num = cell.getNumericCellValue(); if (num == (long) num) { return String.valueOf((long) num); } else { return String.valueOf(num); } } case BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); case FORMULA: // 尝试计算公式结果 try { FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator(); CellValue cellValue = evaluator.evaluate(cell); switch (cellValue.getCellType()) { case NUMERIC: return String.valueOf(cellValue.getNumberValue()); case STRING: return cellValue.getStringValue(); case BOOLEAN: return String.valueOf(cellValue.getBooleanValue()); default: return cell.getCellFormula(); } } catch (Exception e) { return cell.getCellFormula(); } default: return ""; } } /** * 测试方法 */ public static void main(String[] args) { try { // 替换为你的Excel文件路径 File excelFile = new File("D:\\工作\\运维导数据\\溧阳.xlsx"); // 调用解析方法 List> result = parseExcel(excelFile); String s = JsonUtils.objectToJson(result); // 输出结果 System.out.println(s); } catch (Exception e) { e.printStackTrace(); } } }