使用OpenXml将颜色应用于整个Excel行

问题描述 投票:0回答:1

我正在尝试使用OpenXml为整个行着色,但似乎无法获取它。我能够为单个细胞上色。当我添加一行时,如果它是第一行,则尝试应用第四个单元格格式(黄色),否则是正常的。任何帮助将不胜感激。

这是我正在使用的代码:

    public static Row WriteRow(uint rowIndex, WorksheetPart worksheetPart, string[] values)
    {
        var sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
        var lastRow = sheetData.Elements<Row>().LastOrDefault();
        if (lastRow == null || lastRow.RowIndex < rowIndex)
            sheetData.AppendChild(new Row() { RowIndex = rowIndex, CustomFormat = true, StyleIndex = rowIndex == 1u ? 4u : 0u});

        Row sheetRow = sheetData.Elements<Row>().ElementAt((int)rowIndex - 1);

        int colIndex = 1;
        foreach (string value in values)
        {
            sheetRow.AppendChild(
                new Cell()
                {
                    CellReference = $"{GetExcelColumnName(colIndex++)}{rowIndex}",
                    CellValue = new CellValue(value),
                    DataType = CellValues.String
                });
        }

        return sheetRow;
    }

这里是样式表的创建:

public static Stylesheet GenerateStyleSheet()
        {
            return new Stylesheet(
                new Fonts(
                    new Font(                                                               // Index 0 - The default font.
                        new FontSize() { Val = 11 },
                        new Color() { Rgb = new HexBinaryValue() { Value = "00000000" } },
                        new FontName() { Val = "Calibri" }),
                    new Font(                                                               // Index 1 - The bold font.
                        new Bold(),
                        new FontSize() { Val = 11 },
                        new Color() { Rgb = new HexBinaryValue() { Value = "00000000" } },
                        new FontName() { Val = "Calibri" }),
                    new Font(                                                               // Index 2 - The Italic font.
                        new Italic(),
                        new FontSize() { Val = 11 },
                        new Color() { Rgb = new HexBinaryValue() { Value = "00000000" } },
                        new FontName() { Val = "Calibri" }),
                    new Font(                                                               // Index 2 - The Times Roman font. with 16 size
                        new FontSize() { Val = 16 },
                        new Color() { Rgb = new HexBinaryValue() { Value = "00000000" } },
                        new FontName() { Val = "Times New Roman" })
                ),
                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 yellow fill.
                        new PatternFill(
                            new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "FFFFFF00" } }
                        )
                        { 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() { FontId = 0, 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() { FontId = 1, FillId = 0, BorderId = 0, ApplyFont = true },       // Index 1 - Bold 
                    new CellFormat() { FontId = 2, FillId = 0, BorderId = 0, ApplyFont = true },       // Index 2 - Italic
                    new CellFormat() { FontId = 3, FillId = 0, BorderId = 0, ApplyFont = true },       // Index 3 - Times Roman
                    new CellFormat() { FontId = 0, FillId = 2, BorderId = 0, ApplyFill = true },       // Index 4 - Yellow Fill
                    new CellFormat(                                                                   // Index 5 - Alignment
                        new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }
                    )
                    { FontId = 0, FillId = 0, BorderId = 0, ApplyAlignment = true },
                    new CellFormat() { FontId = 0, FillId = 0, BorderId = 1, ApplyBorder = true }      // Index 6 - Border
                )
            ); // return
        }

将样式表分配给工作簿部分

SpreadsheetDocument sheet = XlsxWriterHelper.CreateWorkbook(filePath)
sheet.WorkbookPart.WorkbookStylesPart.Stylesheet = XlsxWriterHelper.GenerateStyleSheet();
sheet.WorkbookPart.WorkbookStylesPart.Stylesheet.Save();
c# excel openxml
1个回答
0
投票

事实证明,在查看OpenXml Productivity Tool]中的“比较文件...”功能之后,我并未在所有适当的位置设置样式。

行上的样式索引仅将该样式应用于该行中尚未定义的单元格。自定义格式也需要设置为true。在行中已定义的单元格已经需要具有已显式分配的样式。这是我的工作代码:

public static Row WriteRow(uint rowIndex, WorksheetPart worksheetPart, string[] values)
{
    var sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
    var lastRow = sheetData.Elements<Row>().LastOrDefault();
    if (lastRow == null || lastRow.RowIndex < rowIndex)
        sheetData.AppendChild(new Row() { RowIndex = rowIndex, CustomFormat = true, StyleIndex = rowIndex == 1u ? 4u : 0u });

    Row sheetRow = sheetData.Elements<Row>().ElementAt((int)rowIndex - 1);

    int colIndex = 1;
    foreach (string value in values)
    {
        sheetRow.AppendChild(
            new Cell()
            {
                CellReference = $"{GetExcelColumnName(colIndex++)}{rowIndex}",
                CellValue = new CellValue(value),
                DataType = CellValues.String,
                StyleIndex = rowIndex == 1u ? 4u : 0u
            });
    }

    return sheetRow;
}
© www.soinside.com 2019 - 2024. All rights reserved.