从DataTable构建表
Contents
[
Hide
]
通常,您的应用程序将从数据库中提取数据并以DataTable的形式存储。 您可能希望轻松地将此数据作为新表插入到文档中,并快速将格式应用于整个表。
请注意,将DataTable中的数据插入文档表的首选方法是使用 Mail Merge与区域. 本文中介绍的技术仅在您无法事先创建合适的模板来合并数据时才会建议使用,换句话说,如果您需要以编程方式执行所有操作。
使用Aspose.Words,您可以轻松地从数据库中检索数据并将其存储为表:
- 在Document上创建一个新的DocumentBuilder对象。
- 使用DocumentBuilder启动一个新表。
- 如果我们想将DataTable中的每个列的名称作为标题行插入,那么遍历每个数据列并将列名写入表中的一行。
- 遍历DataTable中的每个DataRow:
- 遍历DataRow中的每个对象。
- 使用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[]))用于表示图像的字节数组。
- 在数据行的处理结束时,也通过使用DocumentBuilder.endRow()结束由DocumentBuilder创建的行。
- 处理完DataTable中的所有行后,通过调用DocumentBuilder.endTable()完成表。
- 最后,我们可以使用适当的表属性之一(如Table.getStyleIdentifier())设置所需的表样式,以自动将格式应用于整个表。 在这个例子中使用了DataTable中的以下数据:
下面的代码示例演示如何在Aspose.Words中执行上述算法:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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和数据轻松调用该方法。
下面的代码示例演示如何从DataTable
导入数据并将其插入到文档中的新表中:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
下图所示的表格是通过运行上述代码生成的。