Problem merging cells in Header row

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

Hello,
Thank you for your request.
I am a little changed your code. Instead of merging cells from the beginning I merge they after the building of the table complete.

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 (we will merge this cells in future)
    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()
    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()
    'Now we ca execute mail merge
    doc.MailMerge.ExecuteWithRegions(data)

    ' Now you can merge your first header row.
    Dim table As Aspose.Words.Tables.Table = doc.FirstSection.Body.Tables(0)
    For i As Integer = 0 To data.Columns.Count - 1
        Dim cell As Cell = table.Rows.Item(0).Cells.Item(i)
        builder.MoveTo(cell.FirstParagraph)

        If i = 0 Then
            builder.Write("Survey Status Report")
            builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.First
        Else
            If i <> data.Columns.Count Then
                builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.Previous
            Else
                builder.CellFormat.HorizontalMerge = Aspose.Words.Tables.CellMerge.None
            End If
        End If
    Next
End Sub

Hope this helps.

Thanks Victor! :slight_smile: