简体   繁体   English

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

[英]OpenXML Add TableHeader to table in Word Document

I am using .Net to create a Word Document with a dynamic table.我正在使用 .Net 创建一个带有动态表的 Word 文档。 This document can span multiple pages.该文档可以跨越多个页面。 I would like to add a table header to the table, such that every new page will have that header.我想在表格中添加一个表格标题,这样每个新页面都会有这个标题。 I found this documentation, but nothing else: https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.tableheader?view=openxml-2.8.1我找到了这个文档,但没有别的: https : //docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.tableheader?view=openxml-2.8.1

This seems to say you can do this, but does anyone have any code examples: Here is how I currently make my table:这似乎是说你可以做到这一点,但有人有任何代码示例:这是我目前制作表格的方式:

        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)

I found that adding this would use the first row in the table as the header, and repeat it on every new page:我发现添加它会使用表中的第一行作为标题,并在每个新页面上重复它:

    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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM