利用OpenXML获取Excel单元格背景色

最近项目上遇到了关于Excel获取处理的问题,关于Excel单元格背景色的获取,水的文章都大同小异,都没注意到Excel单元格背景色是怎么赋值,这会导致出现有些背景色无法获取的情况。(PS:其实应该叫做前景色)

关于这点我们可以先来看一下,一个Excel文档的内部有关背景色样式代码。

Excel背景色样式解析

  • 这是一个样例Excel,我们给它赋上了一些背景色样式,如下图所示。

  • 但是在Excel的style.xml文件中,这些颜色的表现形式不尽相同。


-<fill> <patternFill patternType="none"/> </fill> -<fill> <patternFill patternType="gray125"/> </fill>

这两种为excel自带的默认填充样式

  • 因此我们可以发现,前三种背景色,Excel中是直接赋予了RGB的色值。然而最后一种颜色却是使用了theme(主题色)和tint(插值)来表示。

    通过以上分析,我们可以得出Excel单元格背景色的两种表现方式:rgbtheme + tint。(PS:关于有的颜色为什么可以直接用rgb,有的颜色用theme加tint的方式表示,本人在微软官方下面提问了,但是没人鸟窝。)

代码实现

  • OK分析完了,上代码。
public string GetCellBackgroundColor(WorkbookPart workbookPart, Cell cell)
{
if (cell == null || cell.StyleIndex == null) return null;
CellFormat cellFormat = (CellFormat)workbookPart.WorkbookStylesPart.Stylesheet.CellFormats.ElementAt((int)cell.StyleIndex.Value);
Fill fill = (Fill)workbookPart.WorkbookStylesPart.Stylesheet.Fills.ElementAt((int)cellFormat.FillId.Value);
PatternFill patternFill = (PatternFill)fill.PatternFill;
ThemePart themePart = workbookPart.ThemePart;
Theme theme = themePart?.Theme;
if (patternFill != null && patternFill.PatternType != null && patternFill.PatternType.Value == PatternValues.Solid)
{
if (patternFill.ForegroundColor != null)
{
if (patternFill.ForegroundColor.Rgb != null)
{
return "#" + patternFill.ForegroundColor.Rgb.Value;
}
else if (patternFill.ForegroundColor.Theme != null)
{
// 主题色获取
string originalColor = ((Color2Type)theme.ThemeElements.ColorScheme.ElementAt((int)patternFill.ForegroundColor.Theme.Value)).RgbColorModelHex.Val;
if (patternFill.ForegroundColor.Tint != null)
{
// 颜色计算
return CalculateTintedColor(originalColor, patternFill.ForegroundColor.Tint);
}
else
{
return "#" + originalColor;
}
}
}
}
return null;
} ublic static string CalculateTintedColor(string originalColor, double tint)
{
// RGB转换
int red = Convert.ToInt32(originalColor.Substring(0, 2), 16);
int green = Convert.ToInt32(originalColor.Substring(2, 2), 16);
int blue = Convert.ToInt32(originalColor.Substring(4, 2), 16);
int interpolatedRed = 0;
int interpolatedGreen = 0;
int interpolatedBlue = 0;
// 基于tint正负值的颜色计算
if (tint > 0)
{
// 白色
int white = 255;
// 插值计算
interpolatedRed = (int)Math.Round(red * (1 - tint) + white * tint);
interpolatedGreen = (int)Math.Round(green * (1 - tint) + white * tint);
interpolatedBlue = (int)Math.Round(blue * (1 - tint) + white * tint);
}
else
{
// 黑色
int black = 0;
// 插值计算
interpolatedRed = (int)Math.Round(red * (1 + tint));
interpolatedGreen = (int)Math.Round(green * (1 + tint));
interpolatedBlue = (int)Math.Round(blue * (1 + tint));
// 防止出现计算结果小于0的情况
interpolatedRed = Math.Max(interpolatedRed, black);
interpolatedGreen = Math.Max(interpolatedGreen, black);
interpolatedBlue = Math.Max(interpolatedBlue, black);
}
// 计算结束后转化为16进制颜色
string interpolatedColor = $"#{interpolatedRed:X2}{interpolatedGreen:X2}{interpolatedBlue:X2}";
return interpolatedColor;
}

分享结束,如果这个分享对您有所帮助的话,请点个赞吧!

利用OpenXML获取Excel单元格背景色的更多相关文章

  1. POI获取excel单元格红色字体,淡蓝色前景色的内容

    如果是Microsoft Excel 97-2003 工作表 (.xls) if(31 == cell.getCellStyle().getFillForegroundColor()) //判断单元格 ...

  2. c#怎样获取excel单元格的RGB颜色

    这段时间一直在做office的工作.前2天获取单元格的颜色的问题一直没搞明确. 開始我想用的就是Npoi.主要前一部分的工作都是用Npoi完毕的 row.GetCell(j).CellStyle.Fi ...

  3. POI 设置Excel单元格背景色(setFillForegroundColor)

    背景介绍:使用Java开发信息系统项目,项目中往往会涉及到报表管理部分,而Excel表格首当其冲称为最合适的选择,但是对单元格操作时对于设置单元格的背景颜色却很少提及,本文旨在方便单元格背景颜色设计. ...

  4. 以字符串形式获取excel单元格中的内容

    public static String getCellValue(XSSFCell cell) { if (cell == null) { return ""; } switch ...

  5. 【Excel】Excel根据单元格背景色求和

    例:用公式计算单元格背景色为浅蓝色的数字之和    步骤一: Office 2003 Insert->Name->Define,Names in workbook输入getColor或ge ...

  6. NPOI之Excel——设置单元格背景色

    NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...

  7. excel小技巧-用于测试用例的编号栏:“获取当前单元格的上一格的值+1”=INDIRECT(ADDRESS(ROW()-1,COLUMN()))+1

    编写用例的时候使用,经常修改用例的时候会需要增加.删除.修改条目,如果用下拉更新数值的方式会很麻烦. 1.使用ctrl下拉,增删移动用例的时候,需要每次都去拉,万一列表比较长,会很麻烦 2.使用ROW ...

  8. NPOI设置Excel单元格字体、边框、对齐、背景色

    代码: ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.BorderBottom = BorderStyle.Thin; ce ...

  9. C# 对Excel 单元格格式, 及行高、 列宽、 单元格边框线、 冻结设置

    一.对行高,列宽.单元格边框等的设置 这篇简短的文字对单元格的操作总结的比较全面,特此转载过来. private _Workbook _workBook = null; private Workshe ...

  10. 利用POI获取Excel中图片和图片位置

    利用POI获取Excel中图片和图片位置(支持excel2003or2007多sheet) 转自:http://blog.csdn.net/delongcpp/article/details/8833 ...

随机推荐

  1. Task Execution and Scheduling In SpringBoot

    开天辟地 Task Execution and Scheduling In the absence of an Executor bean in the context, Spring Boot au ...

  2. K8S | 容器和Pod组件

    对比软件安装和运行: 一.场景 作为研发人员,通常自己电脑的系统环境都是非常复杂,在个人的习惯上,是按照下图的模块管理电脑的系统环境: 对于「基础设施」.「主机操作系统」.「系统软件」来说,通常只做配 ...

  3. Spring容器获取Bean的9种方式

    1 前言 随着SpringBoot的普及,Spring的使用也越来越广,在某些场景下,我们无法通过注解或配置的形式直接获取到某个Bean.比如,在某一些工具类.设计模式实现中需要使用到Spring容器 ...

  4. 痞子衡嵌入式:从功耗测试角度了解i.MXRTxxx系列片内SRAM分区电源控制

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是从功耗测试角度了解i.MXRTxxx系列片内SRAM分区电源控制. 我们知道配合 MCU 一起工作的存储器包含 ROM(Flash) 和 ...

  5. .NET写一个自己的Lambda表达式与表达式树

    LambdaExpression继承Expression Expression又继承LambdaExpressio 所以,Expression与 Expression的区别在于:泛型类以静态类型的方法 ...

  6. 掌握把“烂”SQL牢牢关进笼子里的密钥

    摘要:本文通过5个部分内容帮助开发者快速了解GaussDB(DWS) 资源管理机制,让数仓过载烦恼不再,把"烂"SQL牢牢关进笼子里. 本文分享自华为云社区<直播回顾 | 掌 ...

  7. 零基础实现Java直播(二):实现流程

    一.前提条件 在实现Java直播前,请确保: 已在项目中集成 ZEGO Express SDK,详情请参考 快速开始 - 集成. 已在 ZEGO 控制台 创建项目,并申请有效的 AppID 和 App ...

  8. Java原生图片Base64转码与Base64解码

    原文地址 import org.apache.commons.codec.binary.*; import java.io.*; import java.net.*; /** * 将file文件转换为 ...

  9. Linux中的进程页表

    是什么 进程页表是用于管理进程虚拟地址空间和物理内存之间映射关系的数据结构.它记录了进程中每个虚拟页对应的物理页的信息. 什么作用 进程使用进程页表的方式是通过虚拟地址访问内存.当进程访问一个虚拟地址 ...

  10. KVM VM 添加 usb 设备

    制作xml文件 参考链接:https://libvirt.org/formatdomain.html#usb-pci-scsi-devices <hostdev mode='subsystem' ...