首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenXML将TableHeader添加到Word文档中的表格

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

Stack Overflow用户
提问于 2020-03-28 00:57:53
回答 1查看 545关注 0票数 1

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

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

代码语言:javascript
复制
        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)
EN

回答 1

Stack Overflow用户

发布于 2020-03-28 04:47:02

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

代码语言:javascript
复制
    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())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60890642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档