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

C# 在excel中更改单元格的背景颜色,c#,excel,openxml,C#,Excel,Openxml,我需要用文本“change”更改每个单元格的背景色。有什么想法可以用OpenXML实现吗?我只对一个特定单元格(columnName+rowIndex)进行了更改,但并不是对所有值为“更改”的单元格都进行了更改 使用OpenXML是可行的还是需要使用不同的方法?只需使用条件格式。转到条件格式,新规则,仅格式化包含的单元格,将“单元格值”更改为“特定文本”,输入单词“更改”。将填充颜色设置为您选择的格式,点击apply,OK。您可以使用类创建条件格式,为您希望匹配的每个规则添加条件格式 需要在需要

我需要用文本“change”更改每个单元格的背景色。有什么想法可以用OpenXML实现吗?我只对一个特定单元格(columnName+rowIndex)进行了更改,但并不是对所有值为“更改”的单元格都进行了更改


使用OpenXML是可行的还是需要使用不同的方法?

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

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

需要在需要添加到集合的中定义要应用的格式

如果单元格包含“更改”,则以下代码将创建一个新的电子表格,其条件格式为红色背景。它还使用“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();
    }
}
publicstaticvoid createconditional工作簿(字符串文件路径)
{
使用(电子表格文档=电子表格文档)。
创建(文件路径、电子表格文档类型.工作簿))
{
WorkbookPart WorkbookPart=document.AddWorkbookPart();
workbookPart.工作簿=新工作簿();
var worksheetPart=workbookPart.AddNewPart();
工作表部分。工作表=新工作表();
Sheets Sheets=workbookPart.工作簿.AppendChild(新工作表());
Sheet Sheet=new Sheet(){Id=workbookPart.GetIdOfPart(worksheetPart),SheetId=1,Name=“Sheet”};
附页(页);
workbookPart.Workbook.Save();
var sheetData=worksheetPart.Worksheet.AppendChild(new sheetData());
WorkbookStylesPart-stylesPart=workbookPart.AddNewPart();
stylesPart.Stylesheet=新样式表();
填充=新填充(){Count=1U};
DifferentingFormats DifferentingFormats=新的DifferentingFormats(){Count=(uint32值)1U};
ConditionalFormatting ConditionalFormatting=new ConditionalFormatting(){SequenceOfReferences=new ListValue(){InnerText=“A1:XFD1048576”};
DifferentingFormat DifferentingFormat=新的DifferentingFormat();
填充=新填充();
PatternFill PatternFill=新的PatternFill();
BackgroundColor BackgroundColor=new BackgroundColor(){Rgb=new HexBinaryValue(){Value=“ff0000”};
patternFill.Append(背景色);
填充。追加(填充图案);
格式。追加(填充);
differentingformats.Append(differentingformat);
公式公式1=新公式();
公式1.Text=“\”更改\”;
ConditionalFormattingRule ConditionalFormattingRule=新的ConditionalFormattingRule()
{
类型=条件格式值.CellIs,
FormatId=0U,
优先级=1,
运算符=条件格式运算符值。等于
};
附加条件格式规则(公式1);
附加(conditionalFormatting规则);
工作表部分工作表附加(条件格式);
stylesPart.Stylesheet.Append(格式);
随机r=新随机();

对于(uint rowId=1;rowId,问题是我正在接收xml并生成excel文件。因此,当用户打开excel文件时,它应该是最终形式。因此我需要使用代码:)啊,我的错,没有意识到您需要通过openxml使用它