Bou'n Tabel van a DataTable
Dikwels sal jou aansoek data uit’n databasis trek en dit in die vorm van’n DataTable stoor. U kan hierdie data maklik as’n nuwe tabel in u dokument invoeg en formatering vinnig op die hele tabel toepas.
Met behulp van Aspose.Words kan u maklik data uit’n databasis haal en dit as’n tabel stoor:
- Skep’n nuwe DocumentBuilder voorwerp op jou Document.
- Begin’n nuwe tabel met DocumentBuilder.
- As ons die name van elk van die kolomme van ons DataTable as’n kop ry wil invoeg dan iterate deur elke data kolom en skryf die kolom name in’n ry in die tabel.
- Herhaal deur elke DataRow in die DataTable:
- Iterate deur elke voorwerp in die DataRow.
- Voeg die voorwerp in die dokument met DocumentBuilder. Die metode wat gebruik word, hang af van die tipe voorwerp wat ingevoeg word, bv. DocumentBuilder.Writeln vir teks en DocumentBuilder.InsertImage vir’n byte-array wat’n beeld verteenwoordig.
- Aan die einde van die verwerking van die DataRow eindig ook die ry wat deur die DocumentBuilder geskep word deur DocumentBuilder.EndRow te gebruik.
- Sodra al die rye van die DataTable verwerk is, voltooi die tabel deur DocumentBuilder.EndTable te bel.
- Ten slotte kan ons die gewenste tabel styl stel met behulp van een van die toepaslike tabel eienskappe soos Table.StyleIdentifier om outomaties formatering toe te pas op die hele tabel.
Die ImportTableFromDataTable metode aanvaar’n DocumentBuilder voorwerp, die DataTable wat die data en’n vlag wat spesifiseer of die kolom kop van die DataTable is ingesluit aan die bokant van die tabel. Hierdie metode bou’n tabel van hierdie parameters met behulp van die bouer se huidige posisie en formatering. Verskaf’n metode om data van die DataTable
in te voer en dit in’n nuwe tabel in te voeg met behulp van die DocumentBuilder.
Die volgende data in ons DataTable word in hierdie voorbeeld gebruik:
Die volgende kode voorbeeld toon hoe om die bogenoemde algoritme uit te voer in 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; | |
} |
Die metode kan dan maklik genoem word met behulp van jou DocumentBuilder en data.
Die volgende kode voorbeeld toon hoe om die data van a DataTable
invoer en voeg dit in’n nuwe tabel in die dokument:
// 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"); |