2009-06-18 11 views
15

OpenXml SDK 2.0を使用してExcel文書を作成しましたが、今はスタイルを設定する必要がありますが、できません。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; 
} 

答えて

18

注:OpenXMLの2.0 SDKは、CTPに現在あるとOFFICE2010まで生産使用のためにライセンスされていません。

OpenXML SDKを扱う一般的な方法は、(背景色のように)実装方法を学び、SDKのOpenXmlDiffを使って必要な変更を確認する機能だけを持つ文書と空白の文書を作成することですこの機能を実装するようにしてください。

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

デフォルトで開始:私はサイズ12と赤の背景(索引値64)を使用して新しいフィルの新しいフォントを追加し、新しいのインデックスを参照する新しいCellFormatsを追加しました

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 }, ... 

フォントと塗りつぶし。その後

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; 
+0

+1ありがとうございました。あなたの投稿は私に本当に良いスタートを与えました。 – horgh

2

セルスタイルの指定方法は?ここ

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

「は11U」は、各CellFormat塗りつぶしと境界線のスタイル、のNumberFormat、フォントの組み合わせを定義するStylesPart.Stylesheet.CellFormatsの0から始まるインデックスです。

プログラムですべてのスタイルを追加する必要はなく、必要なすべての書式のテンプレートxlsxファイルを作成し、プログラムでスタイルインデックスを指定することができます。

+0

こんにちはHailaing王。私はあなたがセルのスタイルを変更する方法についてあなたが言及したことからはわかりません。あなたはどのようにStyleIndex = 11Uを得ますか?私が私の場合に必要とするのは、細胞に境界線を持たせることです。私はまた、青い背景を持つ必要があります。 http://stackoverflow.com/questions/15791732/openxml-sdk-having-borders-for-cellに返信できますか?事前に感謝してください –

9

この記事に感謝します。苦労(およびグーグル)の多くの後

、私は最終的にデータセットとファイル名をとり非常に簡単に使用できるC#クラスを作成するには、管理、およびデータセットのデータを含む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

+0

あなたのブログから:*私はあなたが望むように自由なコードであるので、ライセンスを追加していない*法的現実:*ライセンスの不在は、デフォルトの著作権法が適用されることを意味する*(免責条項 - IANAL - 私は弁護士ではない) –

関連する問題