繁体   English   中英

使用 Open XML SDK 将 .docx Word 文档添加到文字处理文档

[英]Adding a .docx Word document to a word processing document using Open XML SDK

我刚开始使用 Open XML,不确定如何将文件 (.docx/.doc) 插入文字处理文档。

我尝试过类似以下的操作,但在使用 Word 2016 启动文档时出现未知文档错误。

Using wordprocessingDocument As WordprocessingDocument = WordprocessingDocument.Open(memoryStream, True)
    Using addProcessDoc As WordprocessingDocument = WordprocessingDocument.Open(itemView.Items(i).SubItems(3).Text.ToString, True)
        Dim tempPart = addProcessDoc.MainDocumentPart
        wordprocessingDocument.AddPart(tempPart) 'ERROR: Specified argument was out of the range of valid values.
    End Using
End Using

可以使用AltChunk来插入整个文档(如 @VarunRathore here 所述,它允许将 HTML 或 OpenXML 等内容嵌入到另一个文档中:

Imports System.Linq
Imports System.IO
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing

Class Program

    Private Shared Sub Main(args As String())

        Dim targetFileName As String = "c:\Users\Public\Documents\Target.docx"
        Dim sourceFileName As String = "c:\Users\Public\Documents\Source.docx"
        Dim templateFile As String = "c:\Users\Public\Documents\Template.docx"

        ' create target file from template
        File.Delete(targetFileName)
        File.Copy(templateFile, targetFileName)

        ' open target document
        Using myDoc As WordprocessingDocument = 
            WordprocessingDocument.Open(targetFileName, True)

            Dim altChunkId As String = "AltChunkId1"
            Dim mainPart As MainDocumentPart = myDoc.MainDocumentPart
            Dim chunk As AlternativeFormatImportPart = 
                mainPart.AddAlternativeFormatImportPart(
                    AlternativeFormatImportPartType.WordprocessingML, 
                    altChunkId)

            ' feed the source document into the alt chunk
            Using fileStream As FileStream = File.Open(sourceFileName, FileMode.Open)
                chunk.FeedData(fileStream)
            End Using

            ' insert alt chunk after last paragraph of body
            Dim altChunk As New AltChunk()
            altChunk.Id = altChunkId
            mainPart.Document.Body.InsertAfter(
                altChunk, 
                mainPart.Document.Body.Elements(Of Paragraph)().Last())

            ' save the document
            mainPart.Document.Save()
        End Using

    End Sub

End Class

完整代码解释如下: https : //docs.microsoft.com/en-us/office/open-xml/how-to-open-and-add-text-to-a-word-processing-document

创建文档后,必须正确关闭它。 如果不想使用 using 块,则必须在创建后使用以下样式。 重要的是不仅要保存文档的主要部分,而且还必须关闭文档本身。 使用以下模式:

Dim wDocFilepath as string ="C:\myWorddoc.docx"

Dim wDocMain As DocumentFormat.OpenXml.Wordprocessing.Document 
Dim wDoc As DocumentFormat.OpenXml.Packaging.WordprocessingDocument
wDoc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(wDocFilepath, True)

wDocMain = wDoc.MainDocumentPart.Document

If wDoc IsNot Nothing Then
       wDocMain.Save()
       wDoc.Close()
End If

wDoc = Nothing

注意 wDocMain 和 wDoc 是具有两个不同名称空间的对象。 一个有 Save 方法,另一个有 Close 方法。 很容易将它们混合在一起。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM