更改 excel 中单元格的背景颜色

Changing background colour of a cell in excel

我需要更改每个包含文本的单元格的背景颜色 "Change"。有什么想法可以使用 OpenXML 实现吗?我只针对一个特定的单元格(columnName + rowIndex)进行更改,但不是针对所有值为 "Change".

的单元格

是否可以使用 OpenXML 或我需要使用不同的方法?

只需使用条件格式。转到条件格式,新规则,仅格式化包含的单元格,将 "Cell Value" 更改为 "Specific Text",输入单词 "Change"。根据您的选择格式化填充颜色,点击应用,确定。

您可以使用 ConditionalFormatting class, adding ConditionalFormattingRules 为每个要匹配的规则创建条件格式。

要应用的格式需要在DifferentialFormat which needs to be added to the DifferentialFormats集合中定义。

如果单元格包含 "Changes",则以下代码将创建一个具有红色背景条件格式的新电子表格。它还会使用 "Changes" 或 "a" 填充单元格 A1:J20,以表明条件格式有效。

public static void CreateConditionalWorkbook(string filepath)
{
    using (SpreadsheetDocument document = SpreadsheetDocument.
        Create(filepath, SpreadsheetDocumentType.Workbook))
    {
        WorkbookPart workbookPart = document.AddWorkbookPart();
        workbookPart.Workbook = new Workbook();

        var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
        worksheetPart.Worksheet = new Worksheet();

        Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

        Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet" };
        sheets.Append(sheet);

        workbookPart.Workbook.Save();

        var sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());

        WorkbookStylesPart stylesPart = workbookPart.AddNewPart<WorkbookStylesPart>();
        stylesPart.Stylesheet = new Stylesheet();

        Fills fills = new Fills() { Count = 1U };

        DifferentialFormats differentialFormats = new DifferentialFormats() { Count = (UInt32Value)1U };

        ConditionalFormatting conditionalFormatting = new ConditionalFormatting() { SequenceOfReferences = new ListValue<StringValue>() { InnerText = "A1:XFD1048576" } };

        DifferentialFormat differentialFormat = new DifferentialFormat();
        Fill fill = new Fill();
        PatternFill patternFill = new PatternFill();
        BackgroundColor backgroundColor = new BackgroundColor() { Rgb = new HexBinaryValue() { Value = "ff0000" } };
        patternFill.Append(backgroundColor);
        fill.Append(patternFill);
        differentialFormat.Append(fill);
        differentialFormats.Append(differentialFormat);

        Formula formula1 = new Formula();
        formula1.Text = "\"Change\"";

        ConditionalFormattingRule conditionalFormattingRule = new ConditionalFormattingRule()
        {
            Type = ConditionalFormatValues.CellIs,
            FormatId = 0U,
            Priority = 1,
            Operator = ConditionalFormattingOperatorValues.Equal
        };

        conditionalFormattingRule.Append(formula1);

        conditionalFormatting.Append(conditionalFormattingRule);

        worksheetPart.Worksheet.Append(conditionalFormatting);
        stylesPart.Stylesheet.Append(differentialFormats);

        Random r = new Random();
        for (uint rowId = 1; rowId <= 20; rowId++)
        {
            Row row = new Row() { RowIndex = rowId };

            for (int cellId = 0; cellId < 10; cellId++)
            {
                Cell cell = new Cell();
                cell.CellReference = string.Format("{0}{1}", (char)(65 + cellId), rowId);
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(r.Next(2) % 2 == 0 ? "a" : "Change");
                row.Append(cell);
            }

            sheetData.Append(row);
        }

        workbookPart.Workbook.Save();

        document.Close();
    }
}

上面 运行 之后的示例输出是: