ExcelParserUtil.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.ruoyi.common.utils;
  2. import org.apache.poi.ss.usermodel.*;
  3. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  4. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.util.*;
  8. /**
  9. * Excel解析工具类
  10. * 提供静态方法将Excel数据解析为List<Map>结构
  11. * 第一列作为Map的key,其他列作为value
  12. */
  13. public class ExcelParserUtil {
  14. /**
  15. * 解析Excel文件数据
  16. * @param file Excel文件对象
  17. * @return List<Map>结构的数据,第一列作为Map的key
  18. * @throws Exception 如果文件格式不支持或解析过程中出现错误
  19. */
  20. public static List<Map<String, String>> parseExcel(File file) throws Exception {
  21. List<Map<String, String>> resultList = new ArrayList<>();
  22. FileInputStream fis = new FileInputStream(file);
  23. Workbook workbook;
  24. // 根据文件扩展名创建不同的Workbook实例
  25. String fileName = file.getName().toLowerCase();
  26. if (fileName.endsWith(".xlsx")) {
  27. workbook = new XSSFWorkbook(fis);
  28. } else if (fileName.endsWith(".xls")) {
  29. workbook = new HSSFWorkbook(fis);
  30. } else {
  31. fis.close();
  32. throw new IllegalArgumentException("不支持的文件格式,仅支持.xls和.xlsx格式");
  33. }
  34. try {
  35. // 获取第一个工作表
  36. Sheet sheet = workbook.getSheetAt(0);
  37. // 获取第一行作为字段名
  38. Row headerRow = sheet.getRow(0);
  39. if (headerRow == null) {
  40. throw new RuntimeException("Excel文件没有数据");
  41. }
  42. // 读取字段名(第一列)
  43. String keyName = getCellValue(headerRow.getCell(0));
  44. if (keyName == null || keyName.trim().isEmpty()) {
  45. throw new RuntimeException("第一列第一行必须包含字段名");
  46. }
  47. // 读取数据行
  48. for (int i = 1; i <= sheet.getLastRowNum(); i++) {
  49. Row row = sheet.getRow(i);
  50. if (row == null) continue;
  51. // 获取key值(第一列的值)
  52. Cell keyCell = row.getCell(0);
  53. if (keyCell == null) continue;
  54. String keyValue = getCellValue(keyCell);
  55. if (keyValue == null || keyValue.trim().isEmpty()) continue;
  56. // 创建Map存储这一行的数据
  57. Map<String, String> rowMap = new LinkedHashMap<>();
  58. rowMap.put(keyName, keyValue);
  59. // 读取其他列的数据
  60. for (int j = 1; j < headerRow.getLastCellNum(); j++) {
  61. Cell headerCell = headerRow.getCell(j);
  62. if (headerCell == null) continue;
  63. String columnName = getCellValue(headerCell);
  64. if (columnName == null || columnName.trim().isEmpty()) continue;
  65. Cell dataCell = row.getCell(j);
  66. String cellValue = dataCell != null ? getCellValue(dataCell) : "";
  67. rowMap.put(columnName, cellValue);
  68. }
  69. resultList.add(rowMap);
  70. }
  71. } finally {
  72. // 确保资源被关闭
  73. workbook.close();
  74. fis.close();
  75. }
  76. return resultList;
  77. }
  78. /**
  79. * 获取单元格的值
  80. * @param cell 单元格对象
  81. * @return 单元格的字符串表示
  82. */
  83. private static String getCellValue(Cell cell) {
  84. if (cell == null) {
  85. return "";
  86. }
  87. switch (cell.getCellType()) {
  88. case STRING:
  89. return cell.getStringCellValue().trim();
  90. case NUMERIC:
  91. if (DateUtil.isCellDateFormatted(cell)) {
  92. return cell.getDateCellValue().toString();
  93. } else {
  94. // 避免显示为科学计数法
  95. double num = cell.getNumericCellValue();
  96. if (num == (long) num) {
  97. return String.valueOf((long) num);
  98. } else {
  99. return String.valueOf(num);
  100. }
  101. }
  102. case BOOLEAN:
  103. return String.valueOf(cell.getBooleanCellValue());
  104. case FORMULA:
  105. // 尝试计算公式结果
  106. try {
  107. FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
  108. CellValue cellValue = evaluator.evaluate(cell);
  109. switch (cellValue.getCellType()) {
  110. case NUMERIC:
  111. return String.valueOf(cellValue.getNumberValue());
  112. case STRING:
  113. return cellValue.getStringValue();
  114. case BOOLEAN:
  115. return String.valueOf(cellValue.getBooleanValue());
  116. default:
  117. return cell.getCellFormula();
  118. }
  119. } catch (Exception e) {
  120. return cell.getCellFormula();
  121. }
  122. default:
  123. return "";
  124. }
  125. }
  126. /**
  127. * 测试方法
  128. */
  129. public static void main(String[] args) {
  130. try {
  131. // 替换为你的Excel文件路径
  132. File excelFile = new File("D:\\工作\\运维导数据\\溧阳.xlsx");
  133. // 调用解析方法
  134. List<Map<String, String>> result = parseExcel(excelFile);
  135. String s = JsonUtils.objectToJson(result);
  136. // 输出结果
  137. System.out.println(s);
  138. } catch (Exception e) {
  139. e.printStackTrace();
  140. }
  141. }
  142. }