从 DataTable 构建表

Contents
[ ]

通常,您的应用程序将从数据库中提取数据并将其以 DataTable 的形式存储。您可以轻松地将此数据作为新表格插入到文档中,并快速将格式应用于整个表格。

使用 Aspose.Words,您可以轻松地从数据库中检索数据并将其存储为表:

  1. Document 上创建一个新的 DocumentBuilder 对象。
  2. 使用 DocumentBuilder 启动一个新表。
  3. 如果我们想插入 DataTable 中每一列的名称作为标题行,则迭代每个数据列并将列名称写入表中的一行。
  4. 迭代 DataTable 中的每个 DataRow
    1. 迭代 DataRow 中的每个对象。
    2. 使用 DocumentBuilder 将对象插入到文档中。使用的方法取决于要插入的对象的类型,例如用于文本的 DocumentBuilder.Writeln 和用于表示图像的字节数组的 DocumentBuilder.InsertImage
    3. DataRow 处理结束时,DocumentBuilder 使用 DocumentBuilder.EndRow 创建的行也结束。
  5. 处理完 DataTable 中的所有行后,通过调用 DocumentBuilder.EndTable 完成该表。
  6. 最后,我们可以使用适当的表格属性之一(例如 Table.StyleIdentifier)设置所需的表格样式,以自动将格式应用于整个表格。

ImportTableFromDataTable 方法接受 DocumentBuilder 对象、包含数据的 DataTable 以及指定 DataTable 中的列标题是否包含在表顶部的标志。此方法使用构建器的当前位置和格式根据这些参数构建表格。提供一种从 DataTable 导入数据并使用 DocumentBuilder 将其插入新表的方法。

本示例使用了 DataTable 中的以下数据:

how-to-build-a-table-from-a-datatable-aspose-words-net

以下代码示例展示了如何在 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");