【问题标题】:declaring New instance of WordprocessingDocument using OpenXML使用 OpenXML 声明 WordprocessingDocument 的新实例
【发布时间】:2013-05-15 03:18:16
【问题描述】:

我第一次尝试使用开放 XML。我正在关注在这里找到的教程http://www.codeproject.com/Articles/36694/Creation-of-a-Word-2007-document-using-the-Open-XM。当我将变量声明为新的WordprocessingDocument 时,我收到错误:

“类型 WordprocessingDocument.Create 未定义”。

Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Wordprocessing
Imports DocumentFormat.OpenXml.Packaging

Partial Class test

Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    makedoc("file.docx")
End Sub

Private Sub makedoc(ByVal documentfilename As String)
    Using mydoc As New WordprocessingDocument.Create(documentfilename, WordprocessingDocumentType.Document)
        Dim mainPart As MainDocumentPart = mydoc.AddMainDocumentPart()
        'Create Document tree for simple document. 
        mainPart.Document = New Document()
        'Create Body (this element contains
        'other elements that we want to include 
        Dim body As New Body()
        'Create paragraph 
        Dim paragraph As New Paragraph()
        Dim run_paragraph As New Run()
        ' we want to put that text into the output document 
        Dim text_paragraph As New Text("Hello World!")
        'Append elements appropriately. 
        run_paragraph.Append(text_paragraph)
        paragraph.Append(run_paragraph)
        body.Append(paragraph)
        mainPart.Document.Append(body)
        ' Save changes to the main document part. 
        mainPart.Document.Save()
    End Using

【问题讨论】:

    标签: .net vb.net openxml openxml-sdk


    【解决方案1】:

    您不能像这样创建WordDocumentProcessing 的新实例。构造函数受到保护。

    你需要调用CreateOpen方法:

    Using mydoc as WordProcessingDocument = WordProcessingDocument.Create(DocumentFileName, WordprocessingDocumentType.Document)
       ...
    End Using
    

    【讨论】:

    • 感谢您的回复。我很确定我最初是调用 Create 方法并在收到错误后将其删除,只是为了看看没有它会发生什么。我会将其添加回来以确保结果,并让您知道结果。
    • 当我使用 .create 时,我收到一个错误,即 WordprocessingDocument.Create 未定义。我需要导入另一个类吗?
    • 不,只需要你上面提到的三个类。这段代码对我有用。我不明白你发生了什么。
    • 我也是,感谢您的帮助。
    • 我编辑了问题以显示实际代码。对于之前使用 .Create 的错误,我们深表歉意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多