经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » XML相关 » XML » 查看文章
利用OpenXML获取Excel单元格背景色
来源:cnblogs  作者:like_a_star  时间:2023/10/19 10:03:53  对本文有异议

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

最近项目上遇到了关于Excel获取处理的问题,关于Excel单元格背景色的获取,水的文章都大同小异,都没注意到Excel单元格背景色是怎么赋值,这会导致出现有些背景色无法获取的情况。(PS:其实应该叫做前景色)
关于这点我们可以先来看一下,一个Excel文档的内部有关背景色样式代码。

Excel背景色样式解析

  • 这是一个样例Excel,我们给它赋上了一些背景色样式,如下图所示。
  • 但是在Excel的style.xml文件中,这些颜色的表现形式不尽相同。
  1. -<fill>
  2. <patternFill patternType="none"/>
  3. </fill>
  4. -<fill>
  5. <patternFill patternType="gray125"/>
  6. </fill>

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

  • 因此我们可以发现,前三种背景色,Excel中是直接赋予了RGB的色值。然而最后一种颜色却是使用了theme(主题色)和tint(插值)来表示。
    通过以上分析,我们可以得出Excel单元格背景色的两种表现方式:rgbtheme + tint。(PS:关于有的颜色为什么可以直接用rgb,有的颜色用theme加tint的方式表示,本人在微软官方下面提问了,但是没人鸟窝。)

代码实现

  • OK分析完了,上代码。
  1. public string GetCellBackgroundColor(WorkbookPart workbookPart, Cell cell)
  2. {
  3. if (cell == null || cell.StyleIndex == null) return null;
  4. CellFormat cellFormat = (CellFormat)workbookPart.WorkbookStylesPart.Stylesheet.CellFormats.ElementAt((int)cell.StyleIndex.Value);
  5. Fill fill = (Fill)workbookPart.WorkbookStylesPart.Stylesheet.Fills.ElementAt((int)cellFormat.FillId.Value);
  6. PatternFill patternFill = (PatternFill)fill.PatternFill;
  7. ThemePart themePart = workbookPart.ThemePart;
  8. Theme theme = themePart?.Theme;
  9. if (patternFill != null && patternFill.PatternType != null && patternFill.PatternType.Value == PatternValues.Solid)
  10. {
  11. if (patternFill.ForegroundColor != null)
  12. {
  13. if (patternFill.ForegroundColor.Rgb != null)
  14. {
  15. return "#" + patternFill.ForegroundColor.Rgb.Value;
  16. }
  17. else if (patternFill.ForegroundColor.Theme != null)
  18. {
  19. // 主题色获取
  20. string originalColor = ((Color2Type)theme.ThemeElements.ColorScheme.ElementAt((int)patternFill.ForegroundColor.Theme.Value)).RgbColorModelHex.Val;
  21. if (patternFill.ForegroundColor.Tint != null)
  22. {
  23. // 颜色计算
  24. return CalculateTintedColor(originalColor, patternFill.ForegroundColor.Tint);
  25. }
  26. else
  27. {
  28. return "#" + originalColor;
  29. }
  30. }
  31. }
  32. }
  33. return null;
  34. }
  35. ublic static string CalculateTintedColor(string originalColor, double tint)
  36. {
  37. // RGB转换
  38. int red = Convert.ToInt32(originalColor.Substring(0, 2), 16);
  39. int green = Convert.ToInt32(originalColor.Substring(2, 2), 16);
  40. int blue = Convert.ToInt32(originalColor.Substring(4, 2), 16);
  41. int interpolatedRed = 0;
  42. int interpolatedGreen = 0;
  43. int interpolatedBlue = 0;
  44. // 基于tint正负值的颜色计算
  45. if (tint > 0)
  46. {
  47. // 白色
  48. int white = 255;
  49. // 插值计算
  50. interpolatedRed = (int)Math.Round(red * (1 - tint) + white * tint);
  51. interpolatedGreen = (int)Math.Round(green * (1 - tint) + white * tint);
  52. interpolatedBlue = (int)Math.Round(blue * (1 - tint) + white * tint);
  53. }
  54. else
  55. {
  56. // 黑色
  57. int black = 0;
  58. // 插值计算
  59. interpolatedRed = (int)Math.Round(red * (1 + tint));
  60. interpolatedGreen = (int)Math.Round(green * (1 + tint));
  61. interpolatedBlue = (int)Math.Round(blue * (1 + tint));
  62. // 防止出现计算结果小于0的情况
  63. interpolatedRed = Math.Max(interpolatedRed, black);
  64. interpolatedGreen = Math.Max(interpolatedGreen, black);
  65. interpolatedBlue = Math.Max(interpolatedBlue, black);
  66. }
  67. // 计算结束后转化为16进制颜色
  68. string interpolatedColor = $"#{interpolatedRed:X2}{interpolatedGreen:X2}{interpolatedBlue:X2}";
  69. return interpolatedColor;
  70. }

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

原文链接:https://www.cnblogs.com/tutaotao/p/17774027.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号