.net OpenXML将TableHeader添加到Word文档中的表中

.net OpenXML将TableHeader添加到Word文档中的表中,.net,openxml,openxml-sdk,openxml-table,.net,Openxml,Openxml Sdk,Openxml Table,我正在使用.Net创建一个带有动态表的Word文档。此文档可以跨越多个页面。我想在表中添加一个表头,这样每个新页面都会有这个表头。我找到了此文档,但没有其他内容: 这似乎表明您可以做到这一点,但是否有人有任何代码示例:以下是我目前制作表格的方式: Dim table As New Table() Dim tr As TableRow Dim tc As TableCell Dim paragraph As New Paragraph

我正在使用.Net创建一个带有动态表的Word文档。此文档可以跨越多个页面。我想在表中添加一个表头,这样每个新页面都会有这个表头。我找到了此文档,但没有其他内容:

这似乎表明您可以做到这一点,但是否有人有任何代码示例:以下是我目前制作表格的方式:

        Dim table As New Table()
        Dim tr As TableRow
        Dim tc As TableCell
        Dim paragraph As New Paragraph
        Dim tblProp As New TableProperties(
            New TableBorders(
                New TopBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.None), .Size = 0},
                New BottomBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.None), .Size = 0},
                New LeftBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.None), .Size = 0},
                New RightBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.None), .Size = 0},
                New InsideHorizontalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.None), .Size = 0},
                New InsideVerticalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.None), .Size = 0}
                ),
                New TableStyle() With {.Val = "TableGrid"},
                New TableWidth() With {.Width = "5000", .Type = TableWidthUnitValues.Pct}
            )

        table.AppendChild(Of TableProperties)(tblProp)
        For Each acronym As Acronym In listOfAcronyms
            tc = New TableCell
            tr = New TableRow
            tc.Append(New TableCellProperties(New TableCellWidth() With {.Type = TableWidthUnitValues.Dxa, .Width = "2400"}))

            rPr = New RunProperties
            newRun = New Run
            fontSize = New DocumentFormat.OpenXml.Wordprocessing.FontSize
            fontSize.Val = "12pt"
            runFonts1 = New RunFonts() With {.Ascii = "Times New Roman"}
            rPr.Append(runFonts1)
            rPr.Append(fontSize)
            newRun.Append(rPr)
            newRun.Append(New Text(acronym.Abbreviation))
            tc.Append(New Paragraph(newRun))
            tr.Append(tc)

            rPr = New RunProperties
            newRun = New Run
            tc = New TableCell
            fontSize = New DocumentFormat.OpenXml.Wordprocessing.FontSize
            fontSize.Val = "12pt"
            runFonts1 = New RunFonts() With {.Ascii = "Times New Roman"}
            rPr.Append(runFonts1)
            rPr.Append(fontSize)
            newRun.Append(rPr)
            newRun.Append(New Text(acronym.Full_Text))
            tc.Append(New Paragraph(newRun))
            tr.Append(tc)

            table.Append(tr)
        Next
        body.Append(table)

我发现添加此项将使用表中的第一行作为标题,并在每个新页面上重复它:

    Dim tableHeader As New TableHeader
    Dim tblHeaderRowProps As TableRowProperties = New TableRowProperties()
    tblHeaderRowProps.Append(New CantSplit() With {.Val = OnOffOnlyValues.On})
    tblHeaderRowProps.Append(New TableHeader() With {.Val = OnOffOnlyValues.On})

   'code to create rest of the table

    Dim row As TableRow = table.GetFirstChild(Of TableRow)
    If (row.TableRowProperties Is Nothing) Then
        row.TableRowProperties = New TableRowProperties()
    End If
    row.TableRowProperties.AppendChild(New TableHeader())

我发现添加此项将使用表中的第一行作为标题,并在每个新页面上重复它:

    Dim tableHeader As New TableHeader
    Dim tblHeaderRowProps As TableRowProperties = New TableRowProperties()
    tblHeaderRowProps.Append(New CantSplit() With {.Val = OnOffOnlyValues.On})
    tblHeaderRowProps.Append(New TableHeader() With {.Val = OnOffOnlyValues.On})

   'code to create rest of the table

    Dim row As TableRow = table.GetFirstChild(Of TableRow)
    If (row.TableRowProperties Is Nothing) Then
        row.TableRowProperties = New TableRowProperties()
    End If
    row.TableRowProperties.AppendChild(New TableHeader())