Insert a Table from a XML with Aspose.Words

Hi there,
¿Exists a way to create a table into the document Word from a XML format with Aspose.Words?
¿Exists this feature in your roadmap?



Oscar Lama S.

Hi Oscar Lama,

Thanks for your inquiry. Yes, you can create table in Word document using Xml. Please reads XML schema and data into the DataSet using DataSet.ReadXml method. After reading the xml, please use DataTable to create table in MS Word.

The following code demonstrates how to achieve this in Aspose.Words. The ImportTableFromDataTable method accepts a DocumentBuilder object, the DataTable containing the data and a flag which specifies if the column heading from the DataTable are included at the top of the table. This method builds a table from these parameters using the builder’s current position and formatting. See the code of ImportTableFromDataTable in attachment.

Document doc = new Document();
// We can position where we want the table to be inserted and also specify any extra formatting to be
// applied onto the table as well.
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 + "in.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.
doc.Save(MyDir + "Out.docx");