Hi,
Am trying to build a table from a DataTable using the code from this thread:
https://forum.aspose.com/t/97128, but that distorted my table too.
Please let me know how to get around this problem.
Here is the code am using:
Public Sub CopyDataTableToDocument(ByVal doc As Document)
'Get dummy datasource (data table with random number of rows)
Dim data As DataTable = GetDataTable()
'Document builder will be needed to build table in the document
Dim builder As New DocumentBuilder(doc)
'if you use template, you can move documentBuilder cursor to any location
'Using MoveToXXX methods, MoveToBookmark for instance
'Customize builder properties
builder.CellFormat.Borders.LineStyle = LineStyle.[Single]
builder.CellFormat.Borders.LineWidth = 1
builder.Font.Name = "Arial"
builder.Font.Size = 10
'Start building table
builder.StartTable()
'build header row (This is the code I added for main header)
builder.CellFormat.Shading.BackgroundPatternColor = Color.DarkBlue
builder.Font.Bold = True
builder.Font.Italic = True
builder.RowFormat.HeadingFormat = True
For i As Integer = 0 To data.Columns.Count
builder.InsertCell()
If i = 0 Then
builder.Write("Survey Status Report")
builder.CellFormat.HorizontalMerge = Words.Tables.CellMerge.First
Else
If i <> data.Columns.Count - 1 Then
builder.CellFormat.HorizontalMerge = Words.Tables.CellMerge.Previous
Else
builder.CellFormat.HorizontalMerge = Words.Tables.CellMerge.None
End If
End If
Next
builder.EndRow()
'build header row
builder.CellFormat.Shading.BackgroundPatternColor = Color.DarkBlue
builder.Font.Bold = True
builder.Font.Italic = True
builder.RowFormat.HeadingFormat = True
For Each col As DataColumn In data.Columns
builder.InsertCell()
builder.Write(col.ColumnName)
Next
builder.EndRow()
'Starting from here there is two aproach.
'First build entire table using documentBuilder
'another build one row with mergefields and use mail merge to fill table with data
'Here we will use the second aproach
'Build entire table
builder.CellFormat.Shading.BackgroundPatternColor = Color.FromArgb(255, 255, 255, 255)
builder.Font.Bold = False
builder.Font.Italic = False
builder.RowFormat.HeadingFormat = False
For Each col As DataColumn In data.Columns
builder.InsertCell()
'If it is first column we should insert tableStart mergefield
If col.Equals(data.Columns(0)) Then
builder.InsertField(String.Format("MERGEFIELD ""TableStart:{0}""", data.TableName), "")
End If
'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)) Then
builder.InsertField(String.Format("MERGEFIELD ""TableEnd:{0}""", data.TableName), "")
End If
Next
builder.EndRow()
builder.EndTable()
Dim table As Words.Tables.Table = doc.FirstSection.Body.Tables(0)
'MergeCells(table.FirstRow.FirstCell, table.Rows(0).Cells(data.Columns.Count - 1))
'Now we ca execute mail merge
doc.MailMerge.ExecuteWithRegions(data)
End Sub
Thank you