首页 > 解决方案 > 如何使用 openXml 为特定单元格添加颜色?

问题描述

我创建了这样的行和单元格=

行 Row1 = new Row() { RowIndex = 1 };

            Row1.AppendChild(new Cell()
            {
                CellReference = "A1",
                DataType = CellValues.String,
                CellValue = new CellValue("Color this")
            });

谁能告诉我如何在 C# 中使用 openXml 为这个特定的单元格着色?我不想给所有的单元格上色,只给这个特定的单元格上色。

找到解决方案 = https://docs.microsoft.com/en-us/archive/blogs/chrisquon/stylizing-your-excel-worksheets-with-open-xml-2-0

标签: c#openxml

解决方案


您需要为此创建一个PatternFill并设置其背景色或前景色。然后您将需要创建一个引用该PatternFill的 CellFormat。然后将单元格的 StyleIndex 设置为该 CellFormat-index。

在 OpenXML 中处理样式是一件非常痛苦的事情。除非您有充分的理由坚持使用 OpenXML,否则我的建议是改用ClosedXML之类的东西。


推荐阅读