DataTable からテーブルを構築する
多くの場合、アプリケーションはデータベースからデータを取得し、DataTable の形式で保存します。このデータを新しい表としてドキュメントに簡単に挿入し、表全体に書式設定をすばやく適用できます。
Aspose.Words を使用すると、データベースからデータを簡単に取得し、テーブルとして保存できます。
- Document 上に新しい DocumentBuilder オブジェクトを作成します。
- DocumentBuilder を使用して新しいテーブルを開始します。
- DataTable の各列の名前をヘッダー行として挿入する場合は、各データ列を反復処理して、テーブル内の行に列名を書き込みます。
- DataTable 内の各 DataRow を反復処理します。
- DataRow 内の各オブジェクトを反復処理します。
- DocumentBuilder を使用してオブジェクトをドキュメントに挿入します。使用される方法は、挿入されるオブジェクトのタイプによって異なります。たとえば、テキストの場合は DocumentBuilder.Writeln、画像を表すバイト配列の場合は DocumentBuilder.InsertImage です。
- DataRow の処理の終了時に、DocumentBuilder.EndRow を使用して DocumentBuilder によって作成されている行も終了します。
- DataTable からのすべての行が処理されたら、DocumentBuilder.EndTable を呼び出してテーブルを終了します。
- 最後に、Table.StyleIdentifier などの適切なテーブル プロパティの 1 つを使用して目的のテーブル スタイルを設定し、テーブル全体に書式設定を自動的に適用します。
ImportTableFromDataTable メソッドは、DocumentBuilder オブジェクト、データを含む DataTable、および DataTable の列見出しがテーブルの先頭に含まれるかどうかを指定するフラグを受け入れます。このメソッドは、ビルダーの現在の位置と書式設定を使用して、これらのパラメーターからテーブルを構築します。 DataTable
からデータをインポートし、DocumentBuilder を使用して新しいテーブルに挿入するメソッドを提供します。
この例では、DataTable 内の次のデータが使用されます。
次のコード例は、Aspose.Words で上記のアルゴリズムを実行する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
/// <summary> | |
/// Imports the content from the specified DataTable into a new Aspose.Words Table object. | |
/// The table is inserted at the document builder's current position and using the current builder's formatting if any is defined. | |
/// </summary> | |
public Table ImportTableFromDataTable(DocumentBuilder builder, DataTable dataTable, | |
bool importColumnHeadings) | |
{ | |
Table table = builder.StartTable(); | |
// Check if the columns' names from the data source are to be included in a header row. | |
if (importColumnHeadings) | |
{ | |
// Store the original values of these properties before changing them. | |
bool boldValue = builder.Font.Bold; | |
ParagraphAlignment paragraphAlignmentValue = builder.ParagraphFormat.Alignment; | |
// Format the heading row with the appropriate properties. | |
builder.Font.Bold = true; | |
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; | |
// Create a new row and insert the name of each column into the first row of the table. | |
foreach (DataColumn column in dataTable.Columns) | |
{ | |
builder.InsertCell(); | |
builder.Writeln(column.ColumnName); | |
} | |
builder.EndRow(); | |
// Restore the original formatting. | |
builder.Font.Bold = boldValue; | |
builder.ParagraphFormat.Alignment = paragraphAlignmentValue; | |
} | |
foreach (DataRow dataRow in dataTable.Rows) | |
{ | |
foreach (object item in dataRow.ItemArray) | |
{ | |
// Insert a new cell for each object. | |
builder.InsertCell(); | |
switch (item.GetType().Name) | |
{ | |
case "DateTime": | |
// Define a custom format for dates and times. | |
DateTime dateTime = (DateTime) item; | |
builder.Write(dateTime.ToString("MMMM d, yyyy")); | |
break; | |
default: | |
// By default any other item will be inserted as text. | |
builder.Write(item.ToString()); | |
break; | |
} | |
} | |
// After we insert all the data from the current record, we can end the table row. | |
builder.EndRow(); | |
} | |
// We have finished inserting all the data from the DataTable, we can end the table. | |
builder.EndTable(); | |
return table; | |
} |
このメソッドは、DocumentBuilder とデータを使用して簡単に呼び出すことができます。
次のコード例は、DataTable
からデータをインポートし、それをドキュメント内の新しいテーブルに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
// We can position where we want the table to be inserted and specify any extra formatting to the table. | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// We want to rotate the page landscape as we expect a wide table. | |
doc.FirstSection.PageSetup.Orientation = Orientation.Landscape; | |
DataSet ds = new DataSet(); | |
ds.ReadXml(MyDir + "List of people.xml"); | |
// Retrieve the data from our data source, which is stored as a DataTable. | |
DataTable dataTable = ds.Tables[0]; | |
// Build a table in the document from the data contained in the DataTable. | |
Table table = ImportTableFromDataTable(builder, dataTable, true); | |
// We can apply a table style as a very quick way to apply formatting to the entire table. | |
table.StyleIdentifier = StyleIdentifier.MediumList2Accent1; | |
table.StyleOptions = TableStyleOptions.FirstRow | TableStyleOptions.RowBands | TableStyleOptions.LastColumn; | |
// For our table, we want to remove the heading for the image column. | |
table.FirstRow.LastCell.RemoveAllChildren(); | |
doc.Save(ArtifactsDir + "WorkingWithTables.BuildTableFromDataTable.docx"); |