ADataTableからテーブルを作成する

Contents
[ ]

多くの場合、アプリケーションはデータベースからデータを取得し、DataTableの形式で格納します。 このデータを新しいテーブルとして文書に簡単に挿入し、テーブル全体に書式設定をすばやく適用することができます。

Aspose.Wordsを使用すると、データベースからデータを簡単に取得してテーブルとして格納できます:

  1. Documentに新しいDocumentBuilderオブジェクトを作成します。
  2. DocumentBuilderを使用して新しいテーブルを開始します。
  3. DataTableの各列の名前をヘッダー行として挿入する場合は、各データ列を反復処理し、列名をテーブルの行に書き込みます。
  4. DataTable内の各DataRowを反復処理します:
    1. DataRow内の各オブジェクトを反復処理します。
    2. DocumentBuilderを使用してオブジェクトを文書に挿入します。 使用されるメソッドは、挿入されるオブジェクトのタイプに依存します(例:テキストの場合は[DocumentBuilder.writeln()](https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#writeln())、画像を表すバイト配列の場合は[DocumentBuilder.insertImage()](https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/#insertImage(byte[]))。
    3. データ行の処理の終了時には、DocumentBuilder.endRow()を使用してDocumentBuilderによって作成される行も終了します。
  5. DataTableのすべての行が処理されたら、DocumentBuilder.endTable()を呼び出してテーブルを終了します。
  6. 最後に、Table.getStyleIdentifier()などの適切なテーブルプロパティのいずれかを使用して目的のテーブルスタイルを設定し、テーブル全体に書式設定を自動的に適用できます。 この例では、DataTableの次のデータが使用されています:

build-a-table-from-a-datatable-aspose-words-java-1

次のコード例は、Aspose.Wordsで上記のアルゴリズムを実行する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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,
boolean 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.
boolean boldValue = builder.getFont().getBold();
int paragraphAlignmentValue = builder.getParagraphFormat().getAlignment();
// Format the heading row with the appropriate properties.
builder.getFont().setBold(true);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// Create a new row and insert the name of each column into the first row of the table.
for (DataColumn column : dataTable.getColumns())
{
builder.insertCell();
builder.writeln(column.getColumnName());
}
builder.endRow();
// Restore the original formatting.
builder.getFont().setBold(boldValue);
builder.getParagraphFormat().setAlignment(paragraphAlignmentValue);
}
for (DataRow dataRow : (Iterable<DataRow>) dataTable.getRows())
{
for (Object item : dataRow.getItemArray())
{
// Insert a new cell for each object.
builder.insertCell();
switch (item.getClass().getName())
{
case "DateTime":
// Define a custom format for dates and times.
Date dateTime = (Date) item;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM d, yyyy");
builder.write(simpleDateFormat.format(dateTime));
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とdataを使用して簡単に呼び出すことができます。

次のコード例は、DataTableからデータをインポートし、ドキュメント内の新しいテーブルに挿入する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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.getFirstSection().getPageSetup().setOrientation(Orientation.LANDSCAPE);
DataSet ds = new DataSet();
ds.readXml(getMyDir() + "List of people.xml");
// Retrieve the data from our data source, which is stored as a DataTable.
DataTable dataTable = ds.getTables().get(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.setStyleIdentifier(StyleIdentifier.MEDIUM_LIST_2_ACCENT_1);
table.setStyleOptions(TableStyleOptions.FIRST_ROW | TableStyleOptions.ROW_BANDS | TableStyleOptions.LAST_COLUMN);
// For our table, we want to remove the heading for the image column.
table.getFirstRow().getLastCell().removeAllChildren();
doc.save(getArtifactsDir() + "WorkingWithTables.BuildTableFromDataTable.docx");

下の図に示す表は、上記のコードを実行することによって生成されます。

build-a-table-from-a-datatable-aspose-words-java-2