16

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;
} 
4

3 に答える 3

20

注: OpenXML 2.0 SDK は現在 CTP で提供されており、Office2010 まで実稼働での使用は許可されていません。

OpenXML SDK を扱うための私の一般的な方法論は、空白のドキュメントと、実装方法を学びたい機能 (背景色など) だけを含むドキュメントを作成し、SDK の OpenXmlDiff を使用して、実装に必要な変更を確認することです。特徴。

ドキュメントをゼロから作成する場合は、DocumentReflector を使用してデフォルトの Stylesheet オブジェクトのコードを生成し、必要なスタイルを追加できます。

デフォルトから始めます:

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 の新しい Font と赤い背景 (インデックス値 64) の新しい Fill を追加し、新しい Font と Fill のインデックスを参照する新しい CellFormats を追加しました。(カウントも必ず更新してください)

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;
于 2009-06-29T21:12:29.800 に答える
10

この記事に感謝します。

多くの苦労 (およびグーグル) の後、最終的に非常に使いやすい C# クラスを作成することができました。このクラスは、DataSet とファイル名を取り、DataSet のデータを含む Office 2007 .xlsx を作成します。

アプリケーションに「Excel へのエクスポート」機能を追加するプロセスが突然簡単になります。

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

CreateExcelFile.CreateExcelDocument(ds, excelFilename);

以下の Web サイトに、完全なソース コードとその使用例を掲載しました。

これは Visual Studio 2008 C# WinForms アプリケーションですが、VS2010 を実行している場合は、Visual Studio でこのプロジェクトをアップグレードできます。

楽しみ。

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

于 2011-10-27T12:16:50.747 に答える
2

セル スタイルを指定する方法は?

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

ここで、「11U」は StylesPart.Stylesheet.CellFormats のゼロから始まるインデックスで、各 CellFormat は NumberFormat、Font、Fill、および Border スタイルの組み合わせを定義します。

プログラムですべてのスタイルを追加する必要はありません。代わりに、必要なすべての形式を含むテンプレート xlsx ファイルを作成し、プログラムでスタイル インデックスを指定できます。

于 2009-08-18T05:44:33.043 に答える