Zellstile in OpenXML-Tabellenkalkulation (SpreadsheetML)
Habe ich generiert .xlsx-Kalkulationstabelle in C# unter Verwendung des OpenXML SDK, aber kann nicht herausfinden, wie man Formatvorlagen für Zellen arbeiten. Ich habe studiert-Dateien von Excel und können nicht ganz herausfinden, wie es gemacht wird.
Ich bin nun erstellen Sie eine Füllung, die Schaffung eines CellStyleFormat
die Punkte an die Füllung, die Schaffung eines CellFormat
die Punkte an, der index der CellStyleFormat
, dann die Erstellung eines CellStyle
dass Punkte auf die CellFormat
.
Hier ist der code, den ich verwende um das Dokument zu erstellen:
Console.WriteLine("Creating document");
using (var spreadsheet = SpreadsheetDocument.Create("output.xlsx", SpreadsheetDocumentType.Workbook))
{
Console.WriteLine("Creating workbook");
spreadsheet.AddWorkbookPart();
spreadsheet.WorkbookPart.Workbook = new Workbook();
Console.WriteLine("Creating worksheet");
var wsPart = spreadsheet.WorkbookPart.AddNewPart<WorksheetPart>();
wsPart.Worksheet = new Worksheet();
var stylesPart = spreadsheet.WorkbookPart.AddNewPart<WorkbookStylesPart>();
stylesPart.Stylesheet = new Stylesheet();
stylesPart.Stylesheet.Fills = new Fills();
//create a solid red fill
var solidRed = new PatternFill() { PatternType = PatternValues.Solid };
solidRed.AppendChild(new BackgroundColor { Rgb = HexBinaryValue.FromString("FF00FF00") });
stylesPart.Stylesheet.Fills.AppendChild(new Fill { PatternFill = new PatternFill() { PatternType = PatternValues.None } });
stylesPart.Stylesheet.Fills.AppendChild(new Fill { PatternFill = solidRed });
stylesPart.Stylesheet.CellStyleFormats = new CellStyleFormats();
stylesPart.Stylesheet.CellStyleFormats.AppendChild(new CellFormat { FillId = 0, ApplyFill = false });
stylesPart.Stylesheet.CellStyleFormats.AppendChild(new CellFormat { FillId = 1, ApplyFill = true });
stylesPart.Stylesheet.CellFormats = new CellFormats();
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat { FormatId = 0 });
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat { FormatId = 1 });
stylesPart.Stylesheet.CellStyles = new CellStyles();
stylesPart.Stylesheet.CellStyles.AppendChild(new CellStyle { Name = "None", FormatId = 0 });
stylesPart.Stylesheet.CellStyles.AppendChild(new CellStyle { Name = "Solid Red", FormatId = 1 });
stylesPart.Stylesheet.Save();
Console.WriteLine("Creating sheet data");
var sheetData = wsPart.Worksheet.AppendChild(new SheetData());
Console.WriteLine("Adding rows /cells...");
var row = sheetData.AppendChild(new Row());
row.AppendChild(new Cell() { CellValue = new CellValue("This"), DataType = CellValues.String });
row.AppendChild(new Cell() { CellValue = new CellValue("is"), DataType = CellValues.String });
row.AppendChild(new Cell() { CellValue = new CellValue("a"), DataType = CellValues.String });
row.AppendChild(new Cell() { CellValue = new CellValue("test."), DataType = CellValues.String });
sheetData.AppendChild(new Row());
row = sheetData.AppendChild(new Row());
row.AppendChild(new Cell() { CellValue = new CellValue("Value:"), DataType = CellValues.String });
row.AppendChild(new Cell() { CellValue = new CellValue("123"), DataType = CellValues.Number });
row.AppendChild(new Cell() { CellValue = new CellValue("Formula:"), DataType = CellValues.String });
row.AppendChild(new Cell() { CellFormula = new CellFormula("B3"), StyleIndex = 1 }); //
Console.WriteLine("Saving worksheet");
wsPart.Worksheet.Save();
Console.WriteLine("Creating sheet list");
var sheets = spreadsheet.WorkbookPart.Workbook.AppendChild(new Sheets());
sheets.AppendChild(new Sheet() { Id = spreadsheet.WorkbookPart.GetIdOfPart(wsPart), SheetId = 1, Name = "Test" });
Console.WriteLine("Saving workbook");
spreadsheet.WorkbookPart.Workbook.Save();
Console.WriteLine("Done.");
}
Hier ist die generierte XML:
workbook.xml
<?xml version="1.0" encoding="utf-8"?>
<x:workbook xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:sheets>
<x:sheet name="Test" sheetId="1" r:id="Rbad86b8c80844a16" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" />
</x:sheets>
</x:workbook>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:fills>
<x:fill>
<x:patternFill patternType="none" />
</x:fill>
<x:fill>
<x:patternFill patternType="solid">
<x:bgColor rgb="FF00FF00" />
</x:patternFill>
</x:fill>
</x:fills>
<x:cellStyleXfs>
<x:xf fillId="0" applyFill="0" />
<x:xf fillId="1" applyFill="1" />
</x:cellStyleXfs>
<x:cellXfs>
<x:xf xfId="0" />
<x:xf xfId="1" />
</x:cellXfs>
<x:cellStyles>
<x:cellStyle name="None" xfId="0" />
<x:cellStyle name="Solid Red" xfId="1" />
</x:cellStyles>
</x:styleSheet>
worksheets/sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:sheetData>
<x:row>
<x:c t="str"><x:v>This</x:v></x:c>
<x:c t="str"><x:v>is</x:v></x:c>
<x:c t="str"><x:v>a</x:v></x:c>
<x:c t="str"><x:v>test.</x:v></x:c>
</x:row>
<x:row />
<x:row>
<x:c t="str"><x:v>Value:</x:v></x:c>
<x:c t="n"><x:v>123</x:v></x:c>
<x:c t="str"><x:v>Formula:</x:v></x:c>
<x:c s="1"><x:f>B3</x:f></x:c>
</x:row>
</x:sheetData>
</x:worksheet>
Der letzten Zelle der letzten Zeile ist, wo ich bin versuchen, um das Stil.
Diese alle überprüft, richtig, wenn ich es durch das OpenXML SDK Productivity Tool, aber wenn ich versuche, die Datei in Excel öffnen, bekomme ich die folgende Fehlermeldung:
Reparierte Datensätze: Format /xl/styles.xml Teil (Stile)
Arbeitsblatt zeigt dann aber die füllen nicht angewandt wird.
Irgendeine Idee, wie man über die Festsetzung?
InformationsquelleAutor der Frage Polynomial | 2012-06-20
Du musst angemeldet sein, um einen Kommentar abzugeben.
Recht, ich schaffte es, um dies herauszufinden, nach viel Experimentieren.
Es stellt sich heraus, dass excel behält den Formaten 0 und 1 für normale Zellen und "Gray125" Muster füllen jeweils. Die meisten der oben genannten code kann entfernt werden, da brauchen wir nur eine
CellFormat
wirklich.Arbeiten code:
Einige Ratschläge:
Verwenden ClosedXML wenn Sie dies vermeiden wollen ist Wahnsinn.
Kann ich nicht empfehlen, ClosedXML hoch genug, wenn du tust diese Art von Arbeit. Die OpenXML-API und das format ist furchtbar mühsam, mit zu arbeiten auf Ihren eigenen, mit allen Arten von nicht dokumentierten Fällen. ClosedXML nicht so viel Bein Arbeit für Sie. Sie sind auch wirklich toll zu bekommen, bugs schnell behoben.
InformationsquelleAutor der Antwort Polynomial
Eine allgemeinere Antwort, all das fand ich nach dem testen, also keine Dokumentation zu zeigen.
Sobald Sie eine
CellFormats
Sammlung im stylesheet führt Excel eine tiefere überprüfung.CellFormats
kann nicht leer sein, es muss mindestens eineCellFormat
es.Nachdem Sie ein
CellFormat
-, Excel wird sich beschweren, wennFills
,Fonts
oderBorders
Sammlungen sind leer.Ersten
Font
ist verwendet als Standard für die gesamte Arbeitsmappe und auch die Zeile/Spalte-Header in Excel.Excel ignoriert die ersten
CellFormat
, so fügen Sie einfach eine leere.Wenn Sie eine
Border
oderFill
im format Excel auch ignorieren ersteBorder
undFill
, so fügen Sie auch leeren, als das erste Kind inBorders
undFills
.Schließlich, beginnend in der zweiten
CellFormat
(s = "1"
) du bist gut zu gehen.Getestet in Excel 2010.
InformationsquelleAutor der Antwort Luizgrs