從 DataTable 中建構一張表格
經常你應用程式會從資料庫取出資料,以 DataTable 的形式儲存。 您可以輕鬆將此資料插入您的文件中,作為新表格,並快速為整張表格應用格式。
使用 Aspose.Words,您可以輕易地從資料庫中取得資料並儲存在一個表格中:
- 在您的Document上建立一個新的DocumentBuilder物件。
- 以 DocumentBuilder 開始一個新的表格。
- 如果我們想要將 DataTable 中的每一欄名稱插入為標題列,那麼我們應該對每一個資料欄進行迭代,並將這些欄名寫入到表格中的某一行。
- 在 DataRow 中迭代每個 DataTable。
- 在 DataRow 中迭代每個物件。
- 請用 DocumentBuilder 將物件插入文件中。 使用的方法取決於要插入的物件類型,例如:DocumentBuilder.Writeln表示文字,DocumentBuilder.InsertImage表示字節陣列(代表圖像)。
- 在處理 DataRow 的最後也結束由 DocumentBuilder 生成的列,使用 DocumentBuilder.EndRow。
- 一旦所有從 DataTable 的列都被處理完,就透過呼叫 DocumentBuilder.EndTable 來完成這個表。
- 最後,我們可以透過使用適合的表格屬性(例如 Table.StyleIdentifier)來設定所需的表格樣式,並自動將格式化應用到整個表格上。
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"); |