【发布时间】:2015-06-13 00:35:50
【问题描述】:
我是 Open XML 的新手。这是我迄今为止能够实现的目标:
- 创建 Word 文档
- 添加带有一些文本的段落
- 通过更改段落的对齐属性来对齐文本
- 更改字体大小和粗体(开/关)
我正在尝试添加两个具有不同字体大小和理由的段落。 这是我的代码:
Dim FontHeading As New DocumentFormat.OpenXml.Wordprocessing.FontSize
FontHeading.Val = New StringValue("28")
Dim FontSubHeading As New DocumentFormat.OpenXml.Wordprocessing.FontSize
FontSubHeading.Val = New StringValue("24")
Dim wordDocument As WordprocessingDocument = WordprocessingDocument.Create(Server.MapPath("/test.docx"), WordprocessingDocumentType.Document)
Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
mainPart.Document = New Document()
Dim dbody As New Body
dbody.AppendChild(AddParagraph("PREM CORPORATE", FontHeading, FontBold, CenterJustification))
dbody.AppendChild(AddParagraph("Company Incorporation Documents", FontSubHeading, FontBold, CenterJustification))
mainPart.Document.AppendChild(dbody)
mainPart.Document.Save()
wordDocument.Close()
添加段落的功能:
Private Function AddParagraph(ByVal txt As String, ByVal fsize As DocumentFormat.OpenXml.Wordprocessing.FontSize, ByVal fbold As Bold, ByVal pjustification As Justification) As Paragraph
Dim runProp As New RunProperties
runProp.Append(fsize)
runProp.Append(fbold)
Dim run As New Run
run.Append(runProp)
run.Append(New Text(txt))
Dim pp As New ParagraphProperties
pp.Justification = pjustification
Dim p As Paragraph = New Paragraph
p.Append(pp)
p.Append(run)
Return p
End Function
上面的结果是一个空文档。 如果我删除第二个 dbody.AppendChild 行,那么它会成功添加第一段。
请帮助我需要更改/添加什么。
【问题讨论】: