1
0
mirror of synced 2025-11-06 04:20:54 +08:00

excel导出颜色使用设置颜色-by Brath

This commit is contained in:
Foming
2025-05-19 15:41:47 +08:00
parent 5665dce2cd
commit e80bdabc49
2 changed files with 21 additions and 6 deletions

View File

@@ -48,7 +48,7 @@ public class ColorUtil {
if (colorStr.equals("#000")) {
colorStr = "#000000";
}
if (colorStr != null && colorStr.length() >= 6) {
if (colorStr.length() >= 6) {
try {
if (colorStr.length() == 8) {
colorStr = colorStr.substring(2);

View File

@@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import java.util.*;
@@ -137,15 +139,22 @@ public class XlsSheetUtil {
setCellStyleFont(wb, style, v_json);
//bg 背景颜色
if (v_json.containsKey("bg")) {
/* if (v_json.containsKey("bg")) {
String _v = getByDBObject(v_json, "bg");
Short _color = ColorUtil.getColorByStr(_v);
if (_color != null) {
style.setFillForegroundColor(_color);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
}*/
if (v_json.containsKey("bg")) {
String _v = getByDBObject(v_json, "bg");
if (_v != null && !_v.isEmpty()) {
XSSFColor color = new XSSFColor(hex2Rgb(_v), null);
((XSSFCellStyle) style).setFillForegroundColor(color);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
}
//vt 垂直对齐 垂直对齐方式0=居中1=上2=下)
if (v_json.containsKey("vt")) {
Integer _v = getIntByDBObject(v_json, "vt");
@@ -204,14 +213,20 @@ public class XlsSheetUtil {
}
}
}
}
}
}
}
public static byte[] hex2Rgb(String colorStr) {
colorStr = colorStr.replace("#", "");
return new byte[] {
(byte) Integer.parseInt(colorStr.substring(0, 2), 16),
(byte) Integer.parseInt(colorStr.substring(2, 4), 16),
(byte) Integer.parseInt(colorStr.substring(4, 6), 16)
};
}
/**
* 设置边框
*