0

如何将表格(参见下面的代码)插入到 word 文件的特定位置(该位置是段落的文本)。那你可以设置表格单元格的字体和宽度吗?

Dim table As New Table()
Dim tblBorders As New TableBorders()
tblBorders.TopBorder = New TopBorder()
tblBorders.TopBorder.Val = New EnumValue(Of BorderValues)(BorderValues.[Single])
tblBorders.BottomBorder = New BottomBorder()
tblBorders.BottomBorder.Val = New EnumValue(Of BorderValues)(BorderValues.[Single])
tblBorders.LeftBorder = New LeftBorder()
tblBorders.LeftBorder.Val = New EnumValue(Of BorderValues)(BorderValues.[Single])
tblBorders.RightBorder = New RightBorder()
tblBorders.RightBorder.Val = New EnumValue(Of BorderValues)(BorderValues.[Single])
tblBorders.InsideHorizontalBorder = New InsideHorizontalBorder()
tblBorders.InsideHorizontalBorder.Val = BorderValues.[Single]
tblBorders.InsideVerticalBorder = New InsideVerticalBorder()
tblBorders.InsideVerticalBorder.Val = BorderValues.[Single]
tblPr.Append(tblBorders)
table.Append(tblPr)
trnew = New TableRow()
tc = New TableCell(New Paragraph(New Run(New Text("A"))))
trnew.Append(tc)
tc = New TableCell(New Paragraph(New Run(New Text("B"))))
trnew.Append(tc)
tc = New TableCell(New Paragraph(New Run(New Text("C"))))
Dim tcp As New TableCellProperties()
Dim gridSpan As New GridSpan()
gridSpan.Val = 4
tcp.Append(gridSpan)
tc.Append(tcp)
trnew.Append(tc)
tc = New TableCell(New Paragraph(New Run(New Text("D"))))
trnew.Append(tc)
table.Append(trnew)

谢谢!

4

1 回答 1

1

访问文档正文,向下解析正文并插入表格

下面是 C# 代码

        //Body is a class in DocumentFormat.OpenXml.Wordprocessing.Body
        // and document part in DocumentFormat.OpenXml.Packaging.MainDocumentPart
        Body body = doc.MainDocumentPart.Document.Body;

        //Traverse the complete document body paragraph by paragraph
        foreach (Paragraph para in body.Descendants<Paragraph>())
        {
           if(para.InnerText.Contains("text the identifies this para")
           {
               para.InserAfterSelf(Table)
           }               
        }

如果对docx格式不是很了解,请使用openxml生产力工具open xml 2.5版 http://download.microsoft.com/download/5/5/3/553C731E-9333-40FB-ADE3-E02DC9643B31/OpenXMLSDKToolV25 .msi

对于 2.0 版 http://download.microsoft.com/download/2/7/F/27FF6744-D970-4FFB-90B8-5226B2B82E0A/OpenXMLSDKTool.msi

我自己没有使用 openxml 中的表格,但是,在我看来,您必须像 in 一样创建运行tc = New TableCell(New Paragraph(New Run(New Text("A")))),然后添加要运行的属性,然后将运行添加到段落中,然后添加到单元格中 - 请参阅链接 格式表格内的字体 OpenXML C#

于 2013-10-08T14:53:07.050 回答