| | |
| | | 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; |
| | |
| | | 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); |
| | |
| | | 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<>(); |
| | |
| | | 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()); |
| | |
| | | } |
| | | |
| | | map.put("value", value); |
| | | map.put("rowSpan", 1); |
| | | map.put("colSpan", 1); |
| | | return map; |
| | | } |
| | | |