2017-06-26 4 views
0

Ich muss die Hintergrundfarbe jeder Zelle mit Text "ändern" ändern. Irgendwelche Ideen, wie ich das mit OpenXML erreichen kann? Ich habe nur erreicht, dass ich für eine bestimmte Zelle (columnName + rowIndex) geändert habe, aber nicht für alle Zellen, deren Wert "Change" ist.Ändern der Hintergrundfarbe einer Zelle in Excel

Ist es mit OpenXML machbar oder muss ich einen anderen Ansatz verwenden?

Antwort

1

Sie können bedingte Formate mit der Klasse ConditionalFormatting erstellen und für jede Regel, die Sie abgleichen möchten, ConditionalFormattingRules hinzufügen.

Das anzuwendende Format muss in einer DifferentialFormat definiert werden, die zur DifferentialFormats Sammlung hinzugefügt werden muss.

Der folgende Code erstellt eine neue Tabelle mit dem bedingten Format eines roten Hintergrunds, wenn die Zelle "Änderungen" enthält. Es füllt auch Zellen A1: J20 entweder mit "Änderungen" oder "a" auf, um zu zeigen, dass das bedingte Format funktioniert.

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(); 
    } 
} 

Ein Beispiel ausgegeben, nachdem die oben ausgeführt ist: Example output

-1

Verwenden Sie nur bedingte Formatierung. Gehen Sie zu Bedingte Formatierung, Neue Regel, Nur Zellen formatieren, die enthalten, ändern Sie "Zellenwert" in "Spezifischer Text", geben Sie das Wort "Ändern" ein. Formatieren Sie die Füllfarbe für Ihre Auswahl, klicken Sie auf Anwenden, OK.

+1

die Sache ist, dass ich xml erhalte, und ich bin zu erzeugen Excel-Datei. Wenn der Benutzer die Excel-Datei öffnet, sollte es in Endform sein. Also muss ich das mit Code machen :) – Toni

+0

Ah mein Schlechter, habe nicht realisiert, dass du es über openxml brauchst – dwirony

Verwandte Themen