#
Junjie
1 天以前 34b0c36cf609ba295c8f66a85d479f740964a864
src/main/java/com/zy/asrs/utils/MapExcelUtils.java
@@ -9,6 +9,7 @@
import com.zy.common.model.MapNode;
import com.zy.core.enums.MapNodeType;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
@@ -257,12 +258,74 @@
                String levStr = split[0];
                Integer lev = Integer.parseInt(levStr);
                int idx = 0;
                int rowLength = -1;
                int cellLength = -1;
                Row titleRow = sheet.getRow(1);
                for (Cell cell : titleRow) {
                    if (idx < 2) {
                        idx++;
                        continue;
                    }
                    HashMap<String, Object> map = getCellValue(sheet, titleRow, cell);
                    String value = map.get("value").toString();
                    if (!Cools.isEmpty(value)) {
                        rowLength = (int) Double.parseDouble(value);
                    }
                }
                idx = 0;
                for (Row row : sheet) {
                    if (idx < 2) {
                        idx++;
                        continue;
                    }
                    Cell cell = row.getCell(1);
                    if(cell != null) {
                        HashMap<String, Object> map = getCellValue(sheet, row, cell);
                        String value = map.get("value").toString();
                        if (!Cools.isEmpty(value)) {
                            cellLength = (int) Double.parseDouble(value);
                        }
                    }
                }
                for (int j = 0; j < cellLength; j++) {
                    Row row = sheet.getRow(j + 2);
                    List<HashMap<String, Object>> rowData = new ArrayList<>();
                    for (Cell cell : row) {
                        rowData.add(getCellValue(cell));
                    for (int k = 0; k < rowLength; k++) {
                        Cell cell = row.getCell(k + 2);
                        rowData.add(getCellValue(sheet, row, cell));
                    }
                    data.add(rowData);
                }
                int numMergedRegions = sheet.getNumMergedRegions();
                for (int j = 0; j < numMergedRegions; j++) {
                    CellRangeAddress region = sheet.getMergedRegion(j);
                    int rowSpan = region.getLastRow() - region.getFirstRow() + 1;
                    int colSpan = region.getLastColumn() - region.getFirstColumn() + 1;
                    int rowIdx = region.getFirstRow() - 2;
                    int colIdx = region.getFirstColumn() - 2;
                    HashMap<String, Object> map = data.get(rowIdx).get(colIdx);
                    map.put("rowSpan", rowSpan);
                    map.put("colSpan", colSpan);
                    for (int k = region.getFirstRow(); k <= region.getLastRow(); k++) {
                        for (int l = region.getFirstColumn(); l <= region.getLastColumn(); l++) {
                            if(k == region.getFirstRow() && l == region.getFirstColumn()) {
                                continue;
                            }
                            HashMap<String, Object> mapData = data.get(k - 2).get(l - 2);
                            mapData.put("bgColor", "merge");
                        }
                    }
                }
                dataMap.put(lev, data);
@@ -274,9 +337,16 @@
        return dataMap;
    }
    private HashMap<String, Object> getCellValue(Cell cell) {
    private HashMap<String, Object> getCellValue(Sheet sheet, Row row, Cell cell) {
        if (cell == null) {
            return null;
            HashMap<String, Object> map = new HashMap<>();
            map.put("bgColor", "none");
            map.put("cellWidth", "");
            map.put("cellHeight", "");
            map.put("value", "");
            map.put("rowSpan", 1);
            map.put("colSpan", 1);
            return map;
        }
        HashMap<String, Object> map = new HashMap<>();
@@ -284,10 +354,22 @@
        String bgColor = getCellBackgroundColor(cell);
        map.put("bgColor", bgColor);
        int columnIndex = cell.getColumnIndex();
        int columnWidth = sheet.getColumnWidth(columnIndex);//获取列宽
        short rowHeight = row.getHeight(); //获取行高
        map.put("cellWidth", columnWidth);
        map.put("cellHeight", rowHeight);
        String value = "";
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                value = cell.getStringCellValue();
                try {
                    JSONObject jsonObject = JSON.parseObject(cell.getStringCellValue());
                    value = JSON.toJSONString(jsonObject);
                } catch (Exception e) {
                    value = cell.getStringCellValue();
                }
                break;
            case Cell.CELL_TYPE_NUMERIC:
                value = String.valueOf(cell.getNumericCellValue());
@@ -306,6 +388,8 @@
        }
        map.put("value", value);
        map.put("rowSpan", 1);
        map.put("colSpan", 1);
        return map;
    }