在excel

时间:2017-06-26 13:09:09

标签: c# excel openxml

我需要使用文本“更改”更改每个单元格的背景颜色。有关如何使用OpenXML实现这一目标的任何想法?我只改变了一个特定的单元格(columnName + rowIndex),但不是所有值为“Change”的单元格。

是否可以使用OpenXML,或者我需要使用其他方法?

2 个答案:

答案 0 :(得分:2)

您可以使用ConditionalFormatting课程创建条件格式,为您希望匹配的每个规则添加ConditionalFormattingRules

要应用的格式需要在DifferentialFormat中定义,需要将其添加到DifferentialFormats集合中。

如果单元格包含“更改”,则以下代码将创建一个新的电子表格,其条件格式为红色背景。它还使用“更改”或“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();
    }
}

运行上述内容后的示例输出是:Example output

答案 1 :(得分:-1)

只需使用条件格式。转到条件格式,新规则,仅格式化包含的单元格,将“单元格值”更改为“特定文本”,输入单词“更改”。将填充颜色格式化为您的选择,点击“应用”,“确定”。