当前位置: 首页 > news >正文

同一个灰色,POI取出来却是白色:一次Excel颜色解析的踩坑记录

最近在写一个后端转换功能,导入xlsx文件,给他解析成JSON格式。结果测试时发现一个问题:两个看起来一模一样的灰色单元格,代码读出来的颜色不一样。属实给我整懵逼了。
比如下面这两个单元格
image

  • A1:能正常拿到灰值,比如 #808080
  • B1:拿到的是 FFFFFF —— 白的

明明都是手动设置的灰色,样式也一样,拿到的填充颜色确不一样。

我第一反应是前景色/背景色搞混了,于是加了一个判断,前景色拿不到就从背景色拿:

XSSFColor color = (XSSFColor) cellStyle.getFillForegroundColorColor();
if (color == null) {color = (XSSFColor) cellStyle.getFillBackgroundColorColor();
}

最后一试,还是白的。

折腾半天,最后发现关键在于:B1 是个主题色,而 A1 是个普通颜色(换了三个AI,问了半天结合起来才折腾出来)。

主题色是什么?

在 Excel 里用“主题颜色”面板选的颜色,比如“辅助色 3”、“深色 1”这类,都叫主题色。它们不是固定的 RGB 值,而是指向当前工作簿的主题定义。也就是说,换一套主题,这颜色可能就变了。

POI 的 XSSFColor 提供了一个方法判断:

color.isThemed() // 返回 true 表示是主题色

如果是主题色,直接调 .getRGB().getARGB() 是拿不到有效值的,经常就是 nullFFFFFF

得结合 ThemesTable 把真正的颜色算出来。

但这还没完。
即使你从 ThemesTable 拿到了主题里的基础颜色,结果可能还是不对。因为还有一个东西叫 tint

什么是 tint

tint 是 Excel 里用来微调主题颜色明暗的一个参数,取值范围是 -1.01.0

  • tint = 0:原色
  • tint > 0:越接近 1,颜色越亮(往白色混合)
  • tint < 0:越接近 -1,颜色越暗(往黑色混合)

比如你选了个“辅助色 3”是深灰,但 Excel 默认给它加了个 tint = -0.24,意思是在这个主题色基础上再压暗一点。如果你忽略这个值,直接拿原始主题色,就会偏亮,甚至变成白色。

所以,只处理主题色不处理 tint,拿到的颜色也可能有问题

最终解决方案

核心思路:

  1. 判断是否为 isThemed()
  2. 如果是,从 ThemesTable 中取出对应索引的基础 RGB
  3. 再根据 getTint() 值,对颜色做明暗调整

下面是封装好的工具类,可以直接用:

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.model.ThemesTable;
import org.apache.poi.xssf.usermodel.XSSFColor;import java.awt.*;@Slf4j
public class ThemeColorResolver {/*** 获取颜色的ARGB十六进制字符串表示** @param themedColor XSSFColor对象* @param theme       主题表* @return ARGB格式的十六进制字符串(如 " FFFF0000 " 表示红色),如果无法解析则返回null*/public static String getActualColorHex(XSSFColor themedColor, ThemesTable theme) {Color color = resolveActualColor(themedColor, theme);return color != null ? toARGBHex(color) : null;}/*** 获取颜色的RGB十六进制字符串表示(不带Alpha通道)** @param themedColor XSSFColor对象* @param theme       主题表* @return RGB格式的十六进制字符串(如 " FF0000 " 表示红色),如果无法解析则返回null*/public static String getActualColorRGBHex(XSSFColor themedColor, ThemesTable theme) {Color color = resolveActualColor(themedColor, theme);return color != null ? toRGBHex(color) : null;}/*** 获取颜色的java.awt.Color对象** @param themedColor XSSFColor对象* @param theme       主题表* @return 解析后的Color对象,如果无法解析则返回null*/public static Color getActualColor(XSSFColor themedColor, ThemesTable theme) {return resolveActualColor(themedColor, theme);}/*** 核心解析方法*/private static Color resolveActualColor(XSSFColor themedColor, ThemesTable theme) {// 如果不是主题色,直接返回ARGB颜色if (!themedColor.isThemed()) {byte[] argb = themedColor.getARGB();if (argb == null || argb.length < 3) {log.warn("Non-themed color has invalid ARGB value");return null;}return new Color(argb[1] & 0xFF, argb[2] & 0xFF, argb[3] & 0xFF, argb[0] & 0xFF);}// 解析主题颜色try {int themeIndex = themedColor.getTheme();XSSFColor themeColor = theme.getThemeColor(themeIndex);byte[] themeRgb = themeColor.getRGB();if (themeRgb == null || themeRgb.length < 3) {log.warn("Theme color not found for index: {}", themeIndex);return null;}// 获取基础颜色(不考虑tint)Color baseColor = new Color(themeRgb[0] & 0xFF, themeRgb[1] & 0xFF, themeRgb[2] & 0xFF);// 应用tint调整double tint = themedColor.getTint();if (tint != 0.0) {return applyTint(baseColor, tint);}return baseColor;} catch (Exception e) {log.error("Error resolving theme color", e);return null;}}/*** 应用tint值调整颜色*/private static Color applyTint(Color baseColor, double tint) {int r = baseColor.getRed();int g = baseColor.getGreen();int b = baseColor.getBlue();if (tint > 0) {// 变亮(混合白色)r = (int) (r + (255 - r) * tint);g = (int) (g + (255 - g) * tint);b = (int) (b + (255 - b) * tint);} else if (tint < 0) {// 变暗(混合黑色)r = (int) (r * (1 + tint));g = (int) (g * (1 + tint));b = (int) (b * (1 + tint));}// 确保值在0-255范围内r = Math.min(255, Math.max(0, r));g = Math.min(255, Math.max(0, g));b = Math.min(255, Math.max(0, b));return new Color(r, g, b);}/*** 将Color转换为ARGB十六进制字符串*/private static String toARGBHex(Color color) {return String.format("%02X%02X%02X%02X",color.getAlpha(), color.getRed(), color.getGreen(), color.getBlue());}/*** 将Color转换为RGB十六进制字符串(不带Alpha)*/private static String toRGBHex(Color color) {return String.format("%02X%02X%02X",color.getRed(), color.getGreen(), color.getBlue());}
}

使用方式

// 获取工作簿的主题表
ThemesTable themes = workbook instanceof XSSFWorkbook ? ((XSSFWorkbook) workbook).getThemesTable() : null;// 获取单元格样式颜色
XSSFColor fill = (XSSFColor) cellStyle.getFillForegroundColorColor();
Color actualColor = ThemeColorResolver.getActualColor(fill, themes);if (actualColor != null) {String hex = ThemeColorResolver.toHex(actualColor); // 如 #808080
}

总结

  • Excel 的“主题色”不是固定值,不能直接 .getRGB()
  • isThemed() 是第一步判断
  • 最终颜色 = 主题基础色 + tint 调整
http://www.hskmm.com/?act=detail&tid=13150

相关文章:

  • 坤驰科技携国产化MTCA解决方案,亮相大科学装置控制系统研讨会
  • 找出所有项目引用了哪些 NuGet 包、版本号、对应项目路径,并筛选出“同一个包名但版本不同”的情况。
  • PC与基恩士PLC通信的C#实现
  • Excel 表格技能
  • labelme标注后的json文件和原图同步按角度旋转
  • rk3588的ai功能和deepseek
  • EPSON L1300打印机清零教程
  • 「线性代数」矩阵运算与初等变换
  • 移动号码线上复机
  • Uni-App 使用android studio打包最新教程
  • tomcat CPU数量和线程数的关系
  • NASA运货飞船天鹅座再次推迟,航天任务为什么总是“彩排”不断
  • Centos系统切换为光盘本地源
  • 基于Hilbert-Huang变换(HHT)的模态分解与瞬时频率计算
  • NIO
  • python处理Excel单机小程序:匹数据,增强版VLookup
  • var sql 的不同用法
  • CF623B Array GCD
  • Python爬虫实现双色球历史数据抓取
  • 酵母细胞工厂全球调控策略研究进展:从遗传编辑到智能响应
  • Avalonia 根据绑定的数据类型动态选择模板
  • PyTorch图神经网络(一)
  • Python版Sigstore稳定版发布:软件供应链签名新标准
  • 网速带宽概念
  • 跨网传输软件:打通数据孤岛,保障安全流通!
  • 「KDOI-07」能量场
  • 基于LQR控制器的柔性机械臂抑振
  • 202507_QQ_caidundun
  • 国内企业邓白氏编码免费申请流程
  • 在CodeBolcks下wxSmith的C++编程教程——wxSmith教程目录(序言)