OpenXml SDK 2.0を使用してExcelドキュメントを作成する
-
06-07-2019 - |
質問
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を追加しました。 (カウントも更新してください)
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を受け取り、Office 2007 .xlsxを作成しますDataSetのデータが含まれています。
突然、「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
セルスタイルの指定方法
new Cell() { CellReference = "B6", StyleIndex = 11U }
こちら&quot; 11U&quot; StylesPart.Stylesheet.CellFormatsのゼロから始まるインデックスで、各CellFormatはNumberFormat、Font、Fill、Borderの各スタイルの組み合わせを定義します。
プログラムごとにすべてのスタイルを追加する必要はありません。代わりに、必要なすべての形式でテンプレートxlsxファイルを作成し、プログラムでスタイルインデックスを指定できます。