package com.zy.asrs.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.core.common.Cools; import com.core.exception.CoolException; import com.zy.asrs.domain.enums.MapExcelNodeType; 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; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.*; @Component public class MapExcelUtils { public HashMap>> processExcelData(HashMap>>> dataMap) { HashMap>> levMap = new HashMap<>(); for (Map.Entry>>> entry : dataMap.entrySet()) { Integer lev = entry.getKey(); List>> excelData = entry.getValue(); HashMap rowMap = new HashMap<>(); HashMap bayMap = new HashMap<>(); int finalRow = -1; int finalBay = -1; //获取列数据 List> tmpList0 = excelData.get(0); List> tmpList1 = excelData.get(1); for (int i = 2; i < tmpList1.size(); i++) { HashMap tmpBayMap = tmpList1.get(i); if (Cools.isEmpty(tmpBayMap.get("value"))) { continue; } HashMap tmpDistanceMap = tmpList0.get(i); int bay = (int) Double.parseDouble(tmpBayMap.get("value").toString()); JSONObject distanceData = JSON.parseObject(tmpDistanceMap.get("value").toString()); bayMap.put(bay, distanceData); finalBay = bay; } //获取排数据 for (int i = 2; i < excelData.size(); i++) { List> rowData = excelData.get(i); HashMap tmpRowMap = rowData.get(1); if (Cools.isEmpty(tmpRowMap.get("value"))) { continue; } HashMap tmpDistanceMap = rowData.get(0); int row = (int) Double.parseDouble(tmpRowMap.get("value").toString()); JSONObject distanceData = JSON.parseObject(tmpDistanceMap.get("value").toString()); rowMap.put(row, distanceData); finalRow = row; } List> mapList = new ArrayList<>(); //生成二维坐标 for (int row = 1; row <= finalRow; row++) { List rows = new ArrayList<>(); for (int bay = 1; bay <= finalBay; bay++) { MapNode node = new MapNode(); node.setId(row + "-" + bay); node.setRow(row); node.setBay(bay); HashMap nodeData = excelData.get(row + 1).get(bay + 1); String nodeType = nodeData.get("bgColor").toString(); String data = ""; if (nodeType.equals(MapExcelNodeType.MAIN_PATH.flag)) { node.setValue(MapNodeType.MAIN_PATH.id); } else if (nodeType.equals(MapExcelNodeType.NORMAL_PATH.flag)) { node.setValue(MapNodeType.NORMAL_PATH.id); } else if (nodeType.equals(MapExcelNodeType.LIFT.flag)) { node.setValue(MapNodeType.LIFT.id); JSONObject jsonData = JSON.parseObject(nodeData.get("value").toString()); data = jsonData.getString("value"); } else if (nodeType.equals(MapExcelNodeType.CHARGE.flag)) { node.setValue(MapNodeType.CHARGE.id); JSONObject jsonData = JSON.parseObject(nodeData.get("value").toString()); data = jsonData.getString("value"); } else if (nodeType.equals(MapExcelNodeType.STATION.flag)) { node.setValue(MapNodeType.CONVEYOR.id); JSONObject jsonData = JSON.parseObject(nodeData.get("value").toString()); data = jsonData.getString("value"); }else { node.setValue(MapNodeType.DISABLE.id); } node.setData(data); rows.add(node); } mapList.add(rows); } MapNode baseMapNode = null; //生成小车地图坐标间距数据 for (int row = 1; row <= finalRow; row++) { for (int bay = 1; bay <= finalBay; bay++) { MapNode mapNode = mapList.get(row - 1).get(bay - 1); JSONObject rowData = rowMap.get(row); JSONObject bayData = bayMap.get(bay); Integer top = rowData.getInteger("top"); Integer bottom = rowData.getInteger("bottom"); Integer left = bayData.getInteger("left"); Integer right = bayData.getInteger("right"); HashMap nodeData = excelData.get(row + 1).get(bay + 1); String nodeValue = nodeData.get("value").toString(); if (Utils.isJSON(nodeValue)) { JSONObject jsonData = JSON.parseObject(nodeValue); if (jsonData.containsKey("top")) { top = jsonData.getInteger("top"); } if (jsonData.containsKey("bottom")) { bottom = jsonData.getInteger("bottom"); } if (jsonData.containsKey("left")) { left = jsonData.getInteger("left"); } if (jsonData.containsKey("right")) { right = jsonData.getInteger("right"); } if (jsonData.containsKey("xBase")) { //基点 baseMapNode = mapNode; } } mapNode.setTop(top); mapNode.setBottom(bottom); mapNode.setLeft(left); mapNode.setRight(right); } } if (baseMapNode == null) { throw new CoolException("未设置基准点"); } List rowList = new ArrayList<>(); List bayList = new ArrayList<>(); for (int row = 1; row <= finalRow; row++) { rowList.add(row); } for (int bay = 1; bay <= finalBay; bay++) { bayList.add(bay); } if (baseMapNode.getRow() != 1) { Collections.reverse(rowList); } if (baseMapNode.getBay() != 1) { Collections.reverse(bayList); } //生成牛眼Y坐标基准数据 for (int i = 0; i < bayList.size(); i++) { Integer bay = bayList.get(i); int yBase = 10000; for (int j = 0; j < rowList.size(); j++) { Integer row = rowList.get(j); MapNode mapNode = mapList.get(row - 1).get(bay - 1); mapNode.setYBase(yBase); if (baseMapNode.getRow() == 1) { yBase += mapNode.getBottom(); }else { yBase += mapNode.getTop(); } } } //生成牛眼X坐标基准数据 for (int i = 0; i < rowList.size(); i++) { Integer row = rowList.get(i); int xBase = 10000; for (int j = 0; j < bayList.size(); j++) { Integer bay = bayList.get(j); MapNode mapNode = mapList.get(row - 1).get(bay - 1); mapNode.setXBase(xBase); if (baseMapNode.getBay() == 1) { xBase += mapNode.getRight(); }else { xBase += mapNode.getLeft(); } } } //生成四向车地图所需格式 MapNode disableNode = new MapNode(); disableNode.setId("0-0"); disableNode.setValue(MapNodeType.DISABLE.id); disableNode.setLeft(0); disableNode.setRight(0); disableNode.setTop(0); disableNode.setBottom(0); List disableRow = new ArrayList<>(); for (int bay = 1; bay <= finalBay; bay++) { disableRow.add(disableNode); } disableRow.add(0, disableNode); disableRow.add(disableNode); for (List mapNodes : mapList) { mapNodes.add(0, disableNode); mapNodes.add(disableNode); } mapList.add(0, disableRow); mapList.add(disableRow); System.out.println(JSON.toJSONString(mapList, SerializerFeature.DisableCircularReferenceDetect)); levMap.put(lev, mapList); } return levMap; } public HashMap>>> readExcel(String filePath) throws IOException { HashMap>>> dataMap = new HashMap<>(); FileInputStream inputStream = new FileInputStream(new File(filePath)); Workbook workbook; if (filePath.endsWith(".xlsx")) { workbook = new XSSFWorkbook(inputStream); } else if (filePath.endsWith(".xls")) { throw new IllegalArgumentException("文件格式不支持"); } else { throw new IllegalArgumentException("文件格式不支持"); } int numberOfSheets = workbook.getNumberOfSheets(); System.out.println("总共有 " + numberOfSheets + " 个Sheet"); // 遍历所有Sheet for (int i = 0; i < numberOfSheets; i++) { Sheet sheet = workbook.getSheetAt(i); String sheetName = sheet.getSheetName(); List>> data = new ArrayList<>(); if (sheetName.contains("F")) { String[] split = sheetName.split("F"); 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 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 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> rowData = new ArrayList<>(); 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 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 mapData = data.get(k - 2).get(l - 2); mapData.put("bgColor", "merge"); } } } dataMap.put(lev, data); } } workbook.close(); inputStream.close(); return dataMap; } private HashMap getCellValue(Sheet sheet, Row row, Cell cell) { if (cell == null) { HashMap 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 map = new HashMap<>(); 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: 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()); break; case Cell.CELL_TYPE_BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: value = cell.getCellFormula(); break; case Cell.CELL_TYPE_BLANK: value = ""; break; default: value = ""; } map.put("value", value); map.put("rowSpan", 1); map.put("colSpan", 1); return map; } public String getCellBackgroundColor(Cell cell) { if (cell == null) { return "无单元格"; } CellStyle style = cell.getCellStyle(); short bgColorIndex = style.getFillForegroundColor(); // 获取前景色(实际是底色) return getXSSFBackgroundColor(style); } // 处理XSSF (.xlsx) 颜色 private String getXSSFBackgroundColor(CellStyle style) { XSSFColor color = (XSSFColor) style.getFillForegroundColorColor(); if (color == null) { return "default"; } byte[] rgb = color.getRGB(); if (rgb == null) { return "default"; } return String.format("RGB(%d,%d,%d)", rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF); } }