From e80bdabc490899b7640539ff8fe547bcf48c13a7 Mon Sep 17 00:00:00 2001 From: Foming Date: Mon, 19 May 2025 15:41:47 +0800 Subject: [PATCH] =?UTF-8?q?excel=E5=AF=BC=E5=87=BA=E9=A2=9C=E8=89=B2?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=AE=BE=E7=BD=AE=E9=A2=9C=E8=89=B2-by=20Bra?= =?UTF-8?q?th?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/reportexcel/util/ColorUtil.java | 2 +- .../reportexcel/util/XlsSheetUtil.java | 25 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/util/ColorUtil.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/util/ColorUtil.java index 5e9037e3..5dbfc300 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/util/ColorUtil.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/util/ColorUtil.java @@ -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); diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/util/XlsSheetUtil.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/util/XlsSheetUtil.java index ed86901a..7e7258c4 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/util/XlsSheetUtil.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/util/XlsSheetUtil.java @@ -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) + }; + } + /** * 设置边框 *