簡體   English   中英

使用OpenXml sdk 2.0創建Excel文檔

[英]Creating Excel document with OpenXml sdk 2.0

我使用OpenXml SDK 2.0創建了一個Excel文檔,現在我必須設置它的樣式,但我不能。

我不知道如何繪制背景顏色或更改不同單元格中的字體大小。

我創建單元格的代碼是:

private static Cell CreateTextCell(string header, string text, UInt32Value index)
{
    Cell c = new Cell();
    c.DataType = CellValues.InlineString;
    c.CellReference = header + index;
    InlineString inlineString = new InlineString();
    DocumentFormat.OpenXml.Spreadsheet.Text t = new DocumentFormat.OpenXml.Spreadsheet.Text();
    t.Text = text;
    inlineString.AppendChild(t);
    c.AppendChild(inlineString);
    return c;
} 

注意:OpenXML 2.0 SDK目前處於CTP狀態,在Office2010之前未獲得生產許可。

我處理OpenXML SDK的一般方法是創建一個空白文檔和一個文檔,其中只包含您想學習如何實現的功能(如背景顏色),並使用SDK的OpenXmlDiff來查看需要進行哪些更改才能實現功能。

如果要從頭開始創建文檔,可以使用DocumentReflector生成默認樣式表對象的代碼,然后添加所需的樣式。

從默認值開始:

new Stylesheet(
new Fonts(
    new Font(
        new FontSize() { Val = 10D },
        new Color() { Theme = (UInt32Value)1U },
        new FontName() { Val = "Arial" },
        new FontFamilyNumbering() { Val = 2 })
) { Count = (UInt32Value)1U },
new Fills(
    new Fill(
        new PatternFill() { PatternType = PatternValues.None }),
    new Fill(
        new PatternFill() { PatternType = PatternValues.Gray125 })
) { Count = (UInt32Value)2U },
new Borders(...
...
...
new CellFormats(
new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }) { Count = (UInt32Value)1U }, ...

我添加了一個大小為12的新字體和一個帶紅色背景的新填充(索引值為64),並添加了新的CellFormats,它引用了新Font和Fill的索引。 (確保也更新計數)

new Stylesheet(
    new Fonts(
        new Font(
            new FontSize() { Val = 10D },
            new Color() { Theme = (UInt32Value)1U },
            new FontName() { Val = "Arial" },
            new FontFamilyNumbering() { Val = 2 }),
        new Font(
            new FontSize() { Val = 12D },
            new Color() { Theme = (UInt32Value)1U },
            new FontName() { Val = "Arial" },
            new FontFamilyNumbering() { Val = 2 })
            ) { Count = (UInt32Value)2U },
    new Fills(
        new Fill(
            new PatternFill() { PatternType = PatternValues.None }),
        new Fill(
            new PatternFill() { PatternType = PatternValues.Gray125 }),
        new Fill(
            new PatternFill() { PatternType = PatternValues.Solid, ForegroundColor = new ForegroundColor() { Rgb = "FFFF0000" }, BackgroundColor = new BackgroundColor() { Indexed = 64 } })
            ) { Count = (UInt32Value)3U },
    new Borders(
        new Border(
            new LeftBorder(), new RightBorder(), new TopBorder(), new BottomBorder(), new DiagonalBorder())
    ) { Count = (UInt32Value)1U },
    new CellStyleFormats(
        new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U }
    ) { Count = (UInt32Value)1U },
    new CellFormats(
        new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U },
        new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)1U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U },
        new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)2U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }
    ) { Count = (UInt32Value)3U },
    new CellStyles(
        new CellStyle() { Name = "Normal", FormatId = (UInt32Value)0U, BuiltinId = (UInt32Value)0U }
    ) { Count = (UInt32Value)1U },
    new DifferentialFormats() { Count = (UInt32Value)0U },
    new TableStyles() { Count = (UInt32Value)0U, DefaultTableStyle = "TableStyleMedium9", DefaultPivotStyle = "PivotStyleLight16" });

然后,在代碼中,我將CellStyle索引應用於我想要格式化的單元格:(單元格A2和A3中已有數據。單元格A2獲得更大的尺寸,A3獲得紅色背景)

SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
sheetData.Descendants<Row>().Where(r => r.RowIndex == 2U).First().Descendants<Cell>().First().StyleIndex = 1U;
sheetData.Descendants<Row>().Where(r => r.RowIndex == 3U).First().Descendants<Cell>().First().StyleIndex = 2U;

非常感謝這篇文章。

在經歷了很多苦苦掙扎(和谷歌搜索)之后,我終於設法創建了一個非常易於使用的C#類,它接受了DataSet和Filename,並創建了一個包含DataSet數據的Office 2007 .xlsx。

突然之間,將應用程序添加到“導出到Excel”的過程變得如此簡單......

DataSet ds = CreateSampleData();                  //  Your code here !
string excelFilename = "C:\\Sample.xlsx";

CreateExcelFile.CreateExcelDocument(ds, excelFilename);

我在以下網站上發布了完整的源代碼,以及使用它的示例。

它是Visual Studio 2008 C#WinForms應用程序,但如果您運行的是VS2010,則可以讓Visual Studio升級此項目。

請享用。

http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

如何指定單元格樣式?

new Cell() { CellReference = "B6", StyleIndex = 11U }

這里“11U”是StylesPart.Stylesheet.CellFormats的從零開始的索引,其中每個CellFormat定義NumberFormat,Font,Fill和Border樣式的組合。

您不必按程序添加所有樣式,而是可以創建包含所需格式的模板xlsx文件,然后在程序中指定樣式索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM