We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Is it possible to bind the table to Word document using Aspose.Words

Hi @,

I have the following requirement, Please let me know if there is any easy way of achieving it using Aspose.Words.

Requirement:

I need to generate a Word document which will have a table with dynamic number of columns. Is it possible to bind a datatable directly to Word document without knowing the number of columns in the datatable.

In MailMerge we need to prepare the template with static columns and map the Datatable columns with Templates Columns in advance. But in my case i will not know how many columns will b thr each time the document gets generated. I will just have the datatable, so i have to take the reference of Datatable for Column names and numberf of Columns.

Is there any direct way by which we can achieve this using Aspose.Words or do we have to code from the scratch by taking a Aspose.Words.Table object?

Please let me know if it is clear or ill try to come up with an example.

Thanks in advance.

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think, in your case, you can try using the same technique as described here:

Hope this helps.

Best regards.

Hi Alexey,

The link which you have provided was really helpful. Thanks a lot for the quick response.

I have one more query related to the same topic:

Is it possible to manage the Column width dynamically from Code?

I tried the following in the code which you have given, For now i am trying to set the width of all Odd Columns to 10 and width of all Even Columns to 30:

int lintColCnt = 0;

//build header row

foreach (DataColumn col in data.Columns)

{

//Clone first cell of first row to build header of the table

Aspose.Words.Tables.Cell hCell = (Aspose.Words.Tables.Cell)myTable.FirstRow.FirstCell.Clone(true);

if (lintColCnt % 2 == 0)

hCell.CellFormat.Width = 10;

else

hCell.CellFormat.Width = 30;

lintColCnt++;

//Insert cell into the first row

myTable.FirstRow.AppendChild(hCell);

//Move document builder cursor to the cell

builder.MoveTo(hCell.FirstParagraph);

//Insert text

builder.Write(col.ColumnName);

}

lintColCnt = 0;

//Build entire table

foreach (DataColumn col in data.Columns)

{

//Clone first cell of the second row to build header of the table

Aspose.Words.Tables.Cell bCell = (Aspose.Words.Tables.Cell)myTable.Rows[1].FirstCell.Clone(true);

if (lintColCnt % 2 == 0)

bCell.CellFormat.Width = 6;

else

bCell.CellFormat.Width = 30;

lintColCnt++;

//Insert cell into the second row

myTable.Rows[1].AppendChild(bCell);

//Move document builder cursor to the cell

builder.MoveTo(bCell.FirstParagraph);

//If it is first column we should insert tableStart mergefield

if (col.Equals(data.Columns[0]))

builder.InsertField(String.Format("MERGEFIELD \"TableStart:{0}\"", data.TableName), "");

//Insert mergefield

builder.InsertField(String.Format("MERGEFIELD \"{0}\"", col.ColumnName), "");

//If column is last we should insert TableEnd

if (col.Equals(data.Columns[data.Columns.Count - 1]))

builder.InsertField(String.Format("MERGEFIELD \"TableEnd:{0}\"", data.TableName), "");

}

I was trying to change the width of alternating columns, but when i see the output doc file, The width of the column is not changing. Width remains same for all the columns.

Please let me know if there is any way by which we control the width of each column.

This is just to make the doc more readable. Some columns require more width and some may require very less width depending on the column Type.

Please suggest us how to proceed, Thanks for your support.

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Your code is correct, but specified width is too small. That is why width of cells is expanded to fill at least one word in the cell. Try specifying width for example 30 and 60.

Best regards.

Got it working, Thanks a lot for the reply.