Compare commits

..

2 Commits

Author SHA1 Message Date
hyc
2276df54b3 Merge pull request '【修改】完善excel考点汉化' (#6) from hyc into master
Reviewed-on: #6
2025-09-11 16:46:31 +08:00
huababa1
14fa679739 【修改】完善excel考点汉化 2025-09-11 16:46:10 +08:00
2 changed files with 138 additions and 8 deletions

View File

@@ -13,14 +13,31 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Map;
public class CellIng { public class CellIng {
// 边框样式英文名称到中文的映射
public final Map<String, String> 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) { public String getLeftBorderStyle(Cell cell, org.apache.poi.ss.usermodel.Workbook wb) {
CellStyle style = cell.getCellStyle(); CellStyle style = cell.getCellStyle();
BorderStyle border = style.getBorderLeft(); 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) { 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) { public String getTopBorderStyle(Cell cell, org.apache.poi.ss.usermodel.Workbook wb) {
CellStyle style = cell.getCellStyle(); CellStyle style = cell.getCellStyle();
BorderStyle border = style.getBorderTop(); 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) { public String getRightBorderStyle(Cell cell, org.apache.poi.ss.usermodel.Workbook wb) {
CellStyle style = cell.getCellStyle(); CellStyle style = cell.getCellStyle();
BorderStyle border = style.getBorderRight(); 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) { public String getBottomBorderStyle(Cell cell, org.apache.poi.ss.usermodel.Workbook wb) {
CellStyle style = cell.getCellStyle(); CellStyle style = cell.getCellStyle();
BorderStyle border = style.getBorderBottom(); BorderStyle border = style.getBorderBottom();
return border != null ? border.name() : ""; return border != null ? borderStyleChineseMap.getOrDefault(border.name() , border.name() ): "";
} }
// 获取下框线颜色 // 获取下框线颜色

View File

@@ -11,9 +11,7 @@ import org.apache.poi.xssf.usermodel.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
public class RangIng { public class RangIng {
@@ -70,7 +68,121 @@ public class RangIng {
return cellValueBuilder.toString(); 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<String, String> 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<String> 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<String> 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;
// 先检查 XSSFxlsx 文件)
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 {
// HSSFxls 文件)
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) { 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) { public String getFillBgColors(String cellRefRange, Workbook workbook, int sheetIndex) {
List<String> cellRefs = getCellRefsInRange(cellRefRange); List<String> cellRefs = getCellRefsInRange(cellRefRange);