使用openxml在word中合并单元格
Merge cell in word using openxml
我尝试使用 openxml.Below 合并单词中的 table 单元格是我的代码,但它没有执行我想要的结果(合并 table 单元格)。
任何人都可以帮助,在此先感谢。
Option Compare Text
Option Explicit On
Option Infer On
Option Strict On
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing
Public Class Form1
Const fileName As String = "C:\Users\ericc\Desktop\test for new task3.docx"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using document = WordprocessingDocument.Open(fileName, True)
Dim doc = document.MainDocumentPart.Document
' Create an empty table.
Dim table As New Table()
' Create a TableProperties object and specify its border information.
Dim tblProp As New TableProperties(New TableBorders(
New TopBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1},
New BottomBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1},
New LeftBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1},
New RightBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1},
New InsideHorizontalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1},
New InsideVerticalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1}))
' Append the TableProperties object to the empty table.
table.AppendChild(Of TableProperties)(tblProp)
' Create a row.
Dim tr As New TableRow()
' Create a cell.
Dim tc1 As New TableCell()
Dim TableCellproperties As TableCellProperties = New TableCellProperties
' Specify the width property of the table cell.
tc1.Append(New TableCellProperties(New TableCellWidth()))
tc1.Append(New TableCellProperties(New HorizontalMerge() With {.Val = MergedCellValues.Restart}))
' Specify the table cell content.
tc1.Append(New Paragraph(New Run(New Text("some text"))))
' Append the table cell to the table row.
tr.Append(tc1)
' Create a second table cell by copying the OuterXml value of the first table cell.
Dim tc2 As New TableCell(tc1.OuterXml)
' Append the table cell to the table row.
tr.Append(tc2)
' Append the table row to the table.
table.Append(tr)
' Append the table to the document.
doc.MainDocumentPart.Document.Body.Append(table)
End Using
Process.Start(fileName)
End Sub
End Class
必须为 table 中的每个单元格设置 HorizontalMerge。
例如,要创建单行 table,其中第一个单元格是常规单元格,第二个单元格由 2 个单元格合并,将单元格 1 的 HorizontalMerge 设置为重新启动,单元格 2 为重新启动,单元格 3 为继续.
在 C# 中看起来像
Table table = New Table();
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.TableCellProperties = new TableCellProperties();
cell1.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Restart };
// Add a content paragraph.
row.Append(cell1);
TableCell cell2 = new TableCell();
cell2.TableCellProperties = new TableCellProperties();
cell2.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Restart };
// Add a content paragraph.
row.Append(cell2);
TableCell cell3 = new TableCell();
cell3.TableCellProperties = new TableCellProperties();
cell3.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Continue };
// Add a content paragraph.
row.Append(cell3);
table.Add(row);
我尝试使用 openxml.Below 合并单词中的 table 单元格是我的代码,但它没有执行我想要的结果(合并 table 单元格)。 任何人都可以帮助,在此先感谢。
Option Compare Text
Option Explicit On
Option Infer On
Option Strict On
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing
Public Class Form1
Const fileName As String = "C:\Users\ericc\Desktop\test for new task3.docx"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using document = WordprocessingDocument.Open(fileName, True)
Dim doc = document.MainDocumentPart.Document
' Create an empty table.
Dim table As New Table()
' Create a TableProperties object and specify its border information.
Dim tblProp As New TableProperties(New TableBorders(
New TopBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1},
New BottomBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1},
New LeftBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1},
New RightBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1},
New InsideHorizontalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1},
New InsideVerticalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 1}))
' Append the TableProperties object to the empty table.
table.AppendChild(Of TableProperties)(tblProp)
' Create a row.
Dim tr As New TableRow()
' Create a cell.
Dim tc1 As New TableCell()
Dim TableCellproperties As TableCellProperties = New TableCellProperties
' Specify the width property of the table cell.
tc1.Append(New TableCellProperties(New TableCellWidth()))
tc1.Append(New TableCellProperties(New HorizontalMerge() With {.Val = MergedCellValues.Restart}))
' Specify the table cell content.
tc1.Append(New Paragraph(New Run(New Text("some text"))))
' Append the table cell to the table row.
tr.Append(tc1)
' Create a second table cell by copying the OuterXml value of the first table cell.
Dim tc2 As New TableCell(tc1.OuterXml)
' Append the table cell to the table row.
tr.Append(tc2)
' Append the table row to the table.
table.Append(tr)
' Append the table to the document.
doc.MainDocumentPart.Document.Body.Append(table)
End Using
Process.Start(fileName)
End Sub
End Class
必须为 table 中的每个单元格设置 HorizontalMerge。
例如,要创建单行 table,其中第一个单元格是常规单元格,第二个单元格由 2 个单元格合并,将单元格 1 的 HorizontalMerge 设置为重新启动,单元格 2 为重新启动,单元格 3 为继续.
在 C# 中看起来像
Table table = New Table();
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.TableCellProperties = new TableCellProperties();
cell1.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Restart };
// Add a content paragraph.
row.Append(cell1);
TableCell cell2 = new TableCell();
cell2.TableCellProperties = new TableCellProperties();
cell2.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Restart };
// Add a content paragraph.
row.Append(cell2);
TableCell cell3 = new TableCell();
cell3.TableCellProperties = new TableCellProperties();
cell3.TableCellProperties.HorizontalMerge = new HorizontalMerge { Val = MergedCellValues.Continue };
// Add a content paragraph.
row.Append(cell3);
table.Add(row);