c# - 无法使用 openxml 在 excel 中设置所需的背景颜色

标签 c# .net excel openxml openxml-sdk

我不熟悉打开 xml 并尝试将标题行的背景颜色设置为灰色,但它始终将其设置为黑色。请引用我正在使用的以下代码。

return new Stylesheet(
            new Fonts(
                new Font(                                                               // Index 0 - The default font.
                    new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 11 },
                    new Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                    new FontName() { Val = "Calibri" }),
                new Font(                                                               // Index 1 - The bold white font.
                    new DocumentFormat.OpenXml.Spreadsheet.Bold(),
                    new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 11 },
                    new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "ffffff" } },
                    new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Calibri" }),

                new Font(                                                               // Index 2 - The bold red font.
                    new DocumentFormat.OpenXml.Spreadsheet.Bold(),
                    new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 11 },
                    new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "ff0000" } },
                    new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Calibri" }),
                new Font(                                                               // Index 2 - The bold red font.
                    new DocumentFormat.OpenXml.Spreadsheet.Bold(),
                    new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 11 },
                    new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
                    new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Calibri" })
            ),
            new Fills(
                new Fill(                                                           // Index 0 - The default fill.
                    new PatternFill() { PatternType = PatternValues.None }),
                new Fill(                                                           // Index 1 - The default fill of gray 125 (required)
                    new PatternFill() { PatternType = PatternValues.Gray125 }),
                new Fill(                                                           // Index 2 - The blue fill.
                    new PatternFill(
                        new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "006699" } }
                    ) { PatternType = PatternValues.Solid }),
                 new Fill(                                                           // Index 3 - The grey fill.
                    new PatternFill(
                        new  BackgroundColor () { Rgb = new HexBinaryValue(){  Value = "808080" } }
                    )
                    { PatternType = PatternValues.Solid }
                   )
            ),
            new Borders(
                new Border(                                                         // Index 0 - The default border.
                    new LeftBorder(),
                    new RightBorder(),
                    new TopBorder(),
                    new BottomBorder(),
                    new DiagonalBorder()),
                new Border(                                                         // Index 1 - Applies a Left, Right, Top, Bottom border to a cell
                    new LeftBorder(
                        new Color() { Auto = true }
                    ) { Style = BorderStyleValues.Thin },
                    new RightBorder(
                        new Color() { Auto = true }
                    ) { Style = BorderStyleValues.Thin },
                    new TopBorder(
                        new Color() { Auto = true }
                    ) { Style = BorderStyleValues.Thin },
                    new BottomBorder(
                        new Color() { Auto = true }
                    ) { Style = BorderStyleValues.Thin },
                    new DiagonalBorder())
            ),
            new CellFormats(
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }) { FontId = 1, FillId = 0, BorderId = 0 },                      // Index 0 - The default cell style.  If a cell does not have a style index applied it will use this style combination instead

                new CellFormat(
                                new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }
                                ) { FontId = 1, FillId = 2, BorderId = 0, ApplyFont = true },   // Index 1 - Bold White Blue Fill

                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }
                                ) { FontId = 2, FillId = 2, BorderId = 0, ApplyFont = true }   , // Index 2 - Bold Red Blue Fill
                new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }
                                )
                { FontId = 3, FillId = 3, BorderId = 0, ApplyFont = true }
            )
        ); // return

生成的输出: enter image description here

期望的输出: enter image description here

请帮助我。提前致谢

最佳答案

PatternFill 的文档(ECMA-376 standard 的第 18.8.32 节)说(强调我的):

For solid cell fills (no pattern), fgColor is used. For cell fills with patterns specified, then the cell fill color is specified by the bgColor element.

您已指定 PatternType = PatternValues.Solid 但您提供的是 BackgroundColor 而不是 ForegroundColor。从 BackgroundColor 更改为 ForegroundColor 将解决您的问题,即这一行:

new BackgroundColor() { Rgb = new HexBinaryValue(){  Value = "808080" } }

应该变成:

new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "808080" } }

关于c# - 无法使用 openxml 在 excel 中设置所需的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42184472/

相关文章:

.net - 运行exe后如何返回退出代码?

regex - 在excel字符串中从右侧提取第一个 float

c# - 为什么编译器生成的 IEnumerator<T> 持有对创建它的实例的引用?

c# - 从 C# 终结器调用静态方法

c# - 在自定义 UserControl 上强制使用透明背景

c# - 如何从 C# 中的特定光标点开始读取文件?

javascript - 即使在 AngularJS 中使用来自 .Net WebApi 的有效 json,$http get 方法也会失败

c# - 以下方法或属性之间的调用不明确(错误??)

excel - VBA 搜索值并从列表中删除(for 循环太慢)

php - 使用纯 PHP 创建多个 Excel 工作表 - 可能吗?