如何将表格(请参阅下面的代码)插入到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)
谢谢!
答案 0 :(得分: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生产力工具 for open xml version 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我自己的表格,但是,在我看来,你必须像tc = New TableCell(New Paragraph(New Run(New Text("A"))))
中那样创建运行,然后添加要运行的属性,然后将运行添加到段落中然后进入细胞 - 见链接
Format Font Inside Table OpenXML C#