【问题标题】:OpenXML Add TableHeader to table in Word DocumentOpenXML 将 TableHeader 添加到 Word 文档中的表格
【发布时间】:2020-03-27 16:57:52
【问题描述】:

我正在使用 .Net 创建带有动态表的 Word 文档。该文档可以跨越多个页面。我想在表格中添加一个表格标题,这样每个新页面都会有这个标题。我找到了这个文档,但没有别的:https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.tableheader?view=openxml-2.8.1

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

        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)

【问题讨论】:

    标签: .net openxml openxml-sdk openxml-table


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多