OpenXML无法着色细胞

nas*_*ski 2 c# openxml openxml-sdk

我试图设置细胞样式,我不能让颜色正常工作,我正在使用以下填充:

// <Fills>
Fill fill0 = new Fill();        // Default fill
Fill fill1 = new Fill(
    new PatternFill(
        new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "DCDCDC" } }
    )
    { PatternType = PatternValues.Solid });

Fills fills = new Fills();      // appending fills
fills.Append(fill0);
fills.Append(fill1);

CellFormat _0_default = new CellFormat() { FontId = 0, FillId = 0, BorderId = 0 }; // Default style : Mandatory | Style ID =0
CellFormat _1_header = new CellFormat() { FontId = 1, FillId = 1, ApplyFill = true }; //HEADER

CellFormats cellformats = new CellFormats();
cellformats.Append(_0_default);
cellformats.Append(_1_header);
Run Code Online (Sandbox Code Playgroud)

这些是我唯一的样式,这是我唯一的填充 - 我将第一行设置为StyleIndex = 1

此外,我制作BackgroundColor或完全省略它似乎并不重要.

从以下链接:https://blogs.msdn.microsoft.com/chrisquon/2009/11/30/stylizing-your-excel-worksheets-with-open-xml-2-0/

但问题是我的细胞现在看起来像这样:

糟糕的模式

你可以看到它不是它应该是灰色的 - 任何想法我错过了什么?谢谢.

nas*_*ski 9

出于某种原因,我似乎无法找到记录,Fill Id 0将始终为None,而Fill Id 1将始终为Gray125.如果你想要一个自定义填充,你将需要至少填写Id 2.如果对此有任何进一步的解释将不胜感激!

            // <Fills>
        Fill fill1 = new Fill(
            new PatternFill(
                new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "DCDCDC" } }
            )
            { PatternType = PatternValues.Solid });

        Fills fills = new Fills(
            new Fill(new PatternFill() { PatternType = PatternValues.None }), //this is what it will be REGARDLESS of what you set it to
            new Fill(new PatternFill() { PatternType = PatternValues.Gray125 }), //this is what it will be REGARDLESS of what you set it to
            fill1);

        // <Borders>
        Border border0 = new Border();     // Default border

        Borders borders = new Borders();    // <APPENDING Borders>
        borders.Append(border0);

        CellFormat _0_default = new CellFormat() { FontId = 0, FillId = 0, BorderId = 0 }; // Default style : Mandatory | Style ID =0
        CellFormat _1_header = new CellFormat() { FontId = 1, FillId = 2, ApplyFill = true }; //HEADER
Run Code Online (Sandbox Code Playgroud)

  • 对于对此感兴趣的任何人,这记录在 ISO/IEC 29500 2.1.704 第 1 部分第 18.8.21 节中,填充:“b. 该标准允许对这些元素进行任意定义:在 Excel 中,前两个 Fill (“[ ISO/IEC-29500-1] §18.8.20;填充") 值被保留,并且不考虑与其预定义值的偏差" (3认同)
  • 我也有完全一样的问题!谢谢你的帖子。 (2认同)