我正在使用.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)发布于 2020-03-28 04:47:02
我发现添加以下代码将使用表中的第一行作为标题,并在每个新页面上重复此操作:
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())https://stackoverflow.com/questions/60890642
复制相似问题