From 14fa679739622f2c731b43b5998d5c20a64b86ce Mon Sep 17 00:00:00 2001 From: huababa1 <2037205722@qq.com> Date: Thu, 11 Sep 2025 16:46:10 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91=E5=AE=8C?= =?UTF-8?q?=E5=96=84excel=E8=80=83=E7=82=B9=E6=B1=89=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exam/service/wpsexcel/cell/CellIng.java | 27 +++- .../exam/service/wpsexcel/range/RangIng.java | 119 +++++++++++++++++- 2 files changed, 138 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/example/exam/exam/service/wpsexcel/cell/CellIng.java b/src/main/java/com/example/exam/exam/service/wpsexcel/cell/CellIng.java index 6823010..3f75caa 100644 --- a/src/main/java/com/example/exam/exam/service/wpsexcel/cell/CellIng.java +++ b/src/main/java/com/example/exam/exam/service/wpsexcel/cell/CellIng.java @@ -13,14 +13,31 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr; import java.lang.reflect.Method; +import java.util.Map; public class CellIng { - + // 边框样式英文名称到中文的映射 + public final Map borderStyleChineseMap = Map.ofEntries( + Map.entry("NONE", "无边框"), + Map.entry("THIN", "细线"), + Map.entry("MEDIUM", "中等线"), + Map.entry("DASHED", "虚线"), + Map.entry("DOTTED", "点线"), + Map.entry("THICK", "粗线"), + Map.entry("DOUBLE", "双线"), + Map.entry("HAIR", "头发线"), + Map.entry("MEDIUMDASHED", "中等虚线"), + Map.entry("DASHDOT", "点划线"), + Map.entry("MEDIUMDASHDOT", "中等点划线"), + Map.entry("DASHDOTDOT", "双点划线"), + Map.entry("MEDIUMDASHDOTDOT", "中等双点划线"), + Map.entry("SLANTEDDASHDOT", "斜点划线") + ); // 获取左框线样式 public String getLeftBorderStyle(Cell cell, org.apache.poi.ss.usermodel.Workbook wb) { CellStyle style = cell.getCellStyle(); BorderStyle border = style.getBorderLeft(); - return border != null ? border.name() : "无"; + return border != null ? borderStyleChineseMap.getOrDefault(border.name() , border.name() ): "无"; } // 获取左框线颜色 public String getLeftBorderColor(Cell cell, org.apache.poi.ss.usermodel.Workbook wb) { @@ -37,7 +54,7 @@ public class CellIng { public String getTopBorderStyle(Cell cell, org.apache.poi.ss.usermodel.Workbook wb) { CellStyle style = cell.getCellStyle(); BorderStyle border = style.getBorderTop(); - return border != null ? border.name() : "无"; + return border != null ? borderStyleChineseMap.getOrDefault(border.name() , border.name() ): "无"; } // 获取上框线颜色 @@ -54,7 +71,7 @@ public class CellIng { public String getRightBorderStyle(Cell cell, org.apache.poi.ss.usermodel.Workbook wb) { CellStyle style = cell.getCellStyle(); BorderStyle border = style.getBorderRight(); - return border != null ? border.name() : "无"; + return border != null ? borderStyleChineseMap.getOrDefault(border.name() , border.name() ): "无"; } // 获取右框线颜色 @@ -71,7 +88,7 @@ public class CellIng { public String getBottomBorderStyle(Cell cell, org.apache.poi.ss.usermodel.Workbook wb) { CellStyle style = cell.getCellStyle(); BorderStyle border = style.getBorderBottom(); - return border != null ? border.name() : "无"; + return border != null ? borderStyleChineseMap.getOrDefault(border.name() , border.name() ): "无"; } // 获取下框线颜色 diff --git a/src/main/java/com/example/exam/exam/service/wpsexcel/range/RangIng.java b/src/main/java/com/example/exam/exam/service/wpsexcel/range/RangIng.java index 9f87248..592b3b2 100644 --- a/src/main/java/com/example/exam/exam/service/wpsexcel/range/RangIng.java +++ b/src/main/java/com/example/exam/exam/service/wpsexcel/range/RangIng.java @@ -11,9 +11,7 @@ import org.apache.poi.xssf.usermodel.*; import java.math.BigDecimal; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.util.*; public class RangIng { @@ -70,7 +68,121 @@ public class RangIng { return cellValueBuilder.toString(); } + //范围 边框→样式 + //范围 边框→颜色 + /** + * 范围 边框 → 样式 + * + * @param cellRef 单元格范围,如 "A1:B2" + * @param workbook Excel 工作簿 + * @param sheetIndex 工作表下标,从 1 开始 + * @return 各单元格边框样式 + */ + public static String getRangeBorderStyles(String cellRef, Workbook workbook, int sheetIndex) { + // 边框样式英文名称到中文的映射 + final Map borderStyleChineseMap = Map.ofEntries( + Map.entry("NONE", "无边框"), + Map.entry("THIN", "细线"), + Map.entry("MEDIUM", "中等线"), + Map.entry("DASHED", "虚线"), + Map.entry("DOTTED", "点线"), + Map.entry("THICK", "粗线"), + Map.entry("DOUBLE", "双线"), + Map.entry("HAIR", "头发线"), + Map.entry("MEDIUMDASHED", "中等虚线"), + Map.entry("DASHDOT", "点划线"), + Map.entry("MEDIUMDASHDOT", "中等点划线"), + Map.entry("DASHDOTDOT", "双点划线"), + Map.entry("MEDIUMDASHDOTDOT", "中等双点划线"), + Map.entry("SLANTEDDASHDOT", "斜点划线") + ); + List cellRefs = getCellRefsInRange(cellRef); + + for (String ref : cellRefs) { + Cell cell = getPoiCellFromRef(workbook, sheetIndex - 1, ref); + if (cell == null) continue; + + CellStyle style = cell.getCellStyle(); + if (style == null) continue; + + BorderStyle[] borders = { + style.getBorderTop(), + style.getBorderBottom(), + style.getBorderLeft(), + style.getBorderRight() + }; + + for (BorderStyle border : borders) { + if (border != BorderStyle.NONE) { + String borderName = border.name(); + return borderStyleChineseMap.getOrDefault(borderName, borderName); + } + } + } + + return "无边框"; + } + + /** + * 范围 边框 → 颜色 + * + * @param cellRef 单元格范围,如 "A1:B2" + * @param workbook Excel 工作簿 + * @param sheetIndex 工作表下标,从 1 开始 + * @return 各单元格边框颜色(RGB或预设颜色名) + */ + public static String getRangeBorderColors(String cellRef, Workbook workbook, int sheetIndex) { + List cellRefs = getCellRefsInRange(cellRef); + + for (String ref : cellRefs) { + Cell cell = getPoiCellFromRef(workbook, sheetIndex-1, ref); + if (cell == null) continue; + + CellStyle style = cell.getCellStyle(); + if (style == null) continue; + + // 先检查 XSSF(xlsx 文件) + if (style instanceof XSSFCellStyle) { + XSSFCellStyle xssfStyle = (XSSFCellStyle) style; + + // 检查四个方向的边框颜色 + XSSFColor[] colors = { + xssfStyle.getTopBorderXSSFColor(), + xssfStyle.getBottomBorderXSSFColor(), + xssfStyle.getLeftBorderXSSFColor(), + xssfStyle.getRightBorderXSSFColor() + }; + + for (XSSFColor color : colors) { + if (color != null && color.getRGB() != null) { + byte[] rgb = color.getRGB(); + String colorName =String.format("%02X%02X%02X", rgb[0], rgb[1], rgb[2]); + return ColorUtils.getChineseColorName(colorName); + } + } + } else { + // HSSF(xls 文件) + short[] borders = { + style.getTopBorderColor(), + style.getBottomBorderColor(), + style.getLeftBorderColor(), + style.getRightBorderColor() + }; + + for (short borderIdx : borders) { + if (borderIdx != IndexedColors.AUTOMATIC.getIndex()) { + IndexedColors color = IndexedColors.fromInt(borderIdx); + if (color != null) { + return ColorUtils.getChineseColorName(color.name()); // 返回预设颜色名称 + } + } + } + } + } + + return "无颜色"; + } // 这里是字体名称示例,先简单返回空或默认 public String getFontName(String cellRef, Workbook workbook, int sheetIndex) { @@ -437,6 +549,7 @@ public class RangIng { + //范围 - 填充 背景色 public String getFillBgColors(String cellRefRange, Workbook workbook, int sheetIndex) { List cellRefs = getCellRefsInRange(cellRefRange);