Erstellen Sie eine Tabelle aus einem DataTable
Oftmals ruft Ihre Anwendung Daten aus einer Datenbank ab und speichert sie in Form einer DataTable-Datei. Sie können diese Daten ganz einfach als neue Tabelle in Ihr Dokument einfügen und die Formatierung schnell auf die gesamte Tabelle anwenden.
Mit Aspose.Words können Sie ganz einfach Daten aus einer Datenbank abrufen und als Tabelle speichern:
- Erstellen Sie ein neues DocumentBuilder-Objekt auf Ihrem Document.
- Starten Sie eine neue Tabelle mit DocumentBuilder.
- Wenn wir die Namen aller Spalten aus unserem DataTable als Kopfzeile einfügen möchten, durchlaufen wir jede Datenspalte und schreiben die Spaltennamen in eine Zeile in der Tabelle.
- Durchlaufen Sie jedes DataRow im DataTable:
- Durchlaufen Sie jedes Objekt im DataRow.
- Fügen Sie das Objekt mit DocumentBuilder in das Dokument ein. Die verwendete Methode hängt vom Typ des einzufügenden Objekts ab, z. B. DocumentBuilder.Writeln für Text und DocumentBuilder.InsertImage für ein Byte-Array, das ein Bild darstellt.
- Am Ende der Verarbeitung des DataRow beenden Sie auch die vom DocumentBuilder erstellte Zeile mithilfe von DocumentBuilder.EndRow.
- Sobald alle Zeilen aus dem DataTable verarbeitet wurden, beenden Sie die Tabelle durch den Aufruf von DocumentBuilder.EndTable.
- Schließlich können wir den gewünschten Tabellenstil mithilfe einer der entsprechenden Tabelleneigenschaften wie Table.StyleIdentifier festlegen, um die Formatierung automatisch auf die gesamte Tabelle anzuwenden.
Die ImportTableFromDataTable-Methode akzeptiert ein DocumentBuilder-Objekt, das DataTable mit den Daten und ein Flag, das angibt, ob die Spaltenüberschriften aus dem DataTable oben in der Tabelle enthalten sind. Diese Methode erstellt aus diesen Parametern eine Tabelle unter Verwendung der aktuellen Position und Formatierung des Builders. Bietet eine Methode zum Importieren von Daten aus dem DataTable
und zum Einfügen dieser mithilfe des DocumentBuilder in eine neue Tabelle.
Die folgenden Daten in unserem DataTable werden in diesem Beispiel verwendet:
Das folgende Codebeispiel zeigt, wie der obige Algorithmus in Aspose.Words ausgeführt wird:
// 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 Methode kann dann einfach mit Ihrem DocumentBuilder und Ihren Daten aufgerufen werden.
Das folgende Codebeispiel zeigt, wie man die Daten aus einem DataTable
importiert und in eine neue Tabelle im Dokument einfügt:
// 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"); |