Răsfoiți Sursa

更新excel导入

chenendian 5 zile în urmă
părinte
comite
ac3d3d3b6a

+ 104 - 3
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/controller/ProjectController.java

@@ -2,6 +2,7 @@
 package com.siwei.apply.controller;
 
 import com.alibaba.fastjson2.JSONArray;
+import com.siwei.apply.common.Constant;
 import com.siwei.apply.domain.Project;
 import com.siwei.apply.domain.res.ProjectCycleRes;
 import com.siwei.apply.domain.res.ProjectOverviewRes;
@@ -10,13 +11,19 @@ import com.siwei.apply.service.ProjectService;
 import com.siwei.common.core.domain.R;
 import com.siwei.common.core.web.controller.BaseController;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.ibatis.annotations.Param;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.File;
+import java.io.FileInputStream;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.*;
 
 /**
  *
@@ -220,7 +227,101 @@ public class ProjectController extends BaseController {
         return R.ok("Client IP: "+ clientIp);
     }
 
+    @GetMapping("/excel")
+    public R<List<Map<String, String>>> getExcelList(@Param("path") String path) {
+        try {
+            if(StringUtils.isBlank(path)){
+                return R.fail("文件路径不能为空");
+            }
+            String uploadFile =  path;
+            Path filePath = Paths.get(uploadFile);
+            File file = filePath.toFile();
+            List<Map<String, String>> res = readExcelToFlatList(file);
+            return R.ok(res);
+        } catch (Exception e) {
+            return R.fail(e.getMessage());
+        }
+    }
 
+    private List<Map<String, String>> readExcelToFlatList(File file) throws Exception {
+        List<Map<String, String>> result = new ArrayList<>();
+        try (FileInputStream fis = new FileInputStream(file);
+             Workbook workbook = new XSSFWorkbook(fis)) {
+            Sheet sheet = workbook.getSheetAt(0);
+            if (sheet == null) {
+                return result;
+            }
+            List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
+            Row subHeaderRow = sheet.getRow(1);
+            if (mergedRegions == null || mergedRegions.isEmpty() || subHeaderRow == null) {
+                Row headerRow = sheet.getRow(0);
+                if (headerRow == null) {
+                    return result;
+                }
+                List<String> headers = new ArrayList<>();
+                for (int col = 0; col < headerRow.getLastCellNum(); col++) {
+                    Cell cell = headerRow.getCell(col);
+                    headers.add(cell == null ? "" : getCellValueAsString(cell).trim().replace("\n", ""));
+                }
+                for (int rowIdx = 1; rowIdx <= sheet.getLastRowNum(); rowIdx++) {
+                    Row dataRow = sheet.getRow(rowIdx);
+                    if (dataRow == null) continue;
+                    Map<String, String> rowMap = new LinkedHashMap<>();
+                    for (int j = 0; j < headers.size(); j++) {
+                        Cell cell = dataRow.getCell(j);
+                        rowMap.put(headers.get(j), cell == null ? "" : getCellValueAsString(cell));
+                    }
+                    result.add(rowMap);
+                }
+                return result;
+            }
+            for (CellRangeAddress region : mergedRegions) {
+                if (region.getFirstRow() != 0) continue;
+                Cell categoryCell = sheet.getRow(0).getCell(region.getFirstColumn());
+                String category = categoryCell == null ? "" : getCellValueAsString(categoryCell).trim().replace("\n", "");
+                if (category.isEmpty()) continue;
+                List<String> subHeaders = new ArrayList<>();
+                for (int col = region.getFirstColumn(); col <= region.getLastColumn(); col++) {
+                    Cell hCell = subHeaderRow.getCell(col);
+                    subHeaders.add(hCell == null ? "" : getCellValueAsString(hCell).trim().replace("\n", ""));
+                }
+                for (int rowIdx = 2; rowIdx <= sheet.getLastRowNum(); rowIdx++) {
+                    Row dataRow = sheet.getRow(rowIdx);
+                    if (dataRow == null) continue;
+                    Map<String, String> rowMap = new LinkedHashMap<>();
+                    for (int j = 0; j < subHeaders.size(); j++) {
+                        Cell cell = dataRow.getCell(region.getFirstColumn() + j);
+                        rowMap.put(category + "." + subHeaders.get(j), cell == null ? "" : getCellValueAsString(cell));
+                    }
+                    result.add(rowMap);
+                }
+            }
+        }
+        return result;
+    }
+
+    private String getCellValueAsString(Cell cell) {
+        if (cell == null) return "";
+        switch (cell.getCellType()) {
+            case STRING:
+                return cell.getStringCellValue();
+            case NUMERIC:
+                if (DateUtil.isCellDateFormatted(cell)) {
+                    return cell.getDateCellValue().toString();
+                }
+                double numVal = cell.getNumericCellValue();
+                if (numVal == (long) numVal) {
+                    return String.valueOf((long) numVal);
+                }
+                return String.valueOf(numVal);
+            case BOOLEAN:
+                return String.valueOf(cell.getBooleanCellValue());
+            case FORMULA:
+                return cell.getCellFormula();
+            default:
+                return "";
+        }
+    }
 
 
 }