Free Support Forum - aspose.com

Convert datatable or dataset to WORD table

Hi - How can I convert a dataTable or dataset to a WORD table. I do not want to do a mailmerge. I want to be able to dynamically create a WORD table from the dataTable/dataset. Thank you.

Hi Medisoftrx,

You can use following function to convert a DataTable object into a word table.

//Add data table into objDocumentBuilder in tabular format
private DocumentBuilder AddTabularData(DocumentBuilder objDocumentBuilder, DataTable objDataTable, double columnWidth)
{
//Insert table using document builder
objDocumentBuilder.StartTable();

//Insert columns one by one
foreach (DataColumn objDataColumn in objDataTable.Columns)
{
objDocumentBuilder.InsertCell();
objDocumentBuilder.CellFormat.Width = columnWidth;
objDocumentBuilder.CellFormat.Borders.LineWidth = 0.8;
objDocumentBuilder.Write(objDataColumn.ColumnName);
}
objDocumentBuilder.EndRow();

//Insert columns data row by row
foreach (DataRow objDataRow in objDataTable.Rows)
{
foreach (object cell in objDataRow.ItemArray)
{
objDocumentBuilder.InsertCell();
objDocumentBuilder.Bold = false;
objDocumentBuilder.Write(cell.ToString());
}
objDocumentBuilder.EndRow();
}

//End table
objDocumentBuilder.EndTable();

return objDocumentBuilder;
}


Regards,
Imran Rafique

Hi there,


Thanks for your inquiry.

As it happens there is a code sample which demonstrates how to achieve this which is due to be released in the documentation within the next week or so. In addition to Imran’s code which also correctly demonstrates how to achieve this, please check it out below.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>DocumentBuilder<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> builder = new DocumentBuilder(doc);<o:p></o:p>

DataTable dataTable = GetData();

// Build a table in the document from the data contained in the DataTable.

Table table = CreateTableFromDataTable(builder, dataTable, true);

// We can apply a table style as a very quick way to format the entire table.

table.StyleIdentifier = StyleIdentifier.MediumList2Accent1;

table.StyleOptions = TableStyleOptions.FirstRow | TableStyleOptions.RowBands | TableStyleOptions.LastColumn;

///

/// Imports the content from the specified DataTable into a new Aspose.Words table object. The table is inserted at the current position

/// of the document builder and using the current builder's formatting if any is defined.

///

public static Table CreateTableFromDataTable(DocumentBuilder builder, DataTable dataTable, bool importColumnHeadings)

{

Document doc = builder.Document;

Table table = builder.StartTable();

// Check if the names of the columns 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 "Byte[]":

// Assume a byte array is an image. Other data types can be added here.

Shape shape = builder.InsertImage((byte[])item, 50, 50);

break;

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 a DataRow when 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;

}


Thanks,