【问题标题】:OpenXML c++/cliOpenXML c++/cli
【发布时间】:2012-10-05 20:33:01
【问题描述】:

我想在 C++/CLI 中使用 OpenXML 创建一个 .docx 文档(Word 2007 和 2010)

我使用此代码生成文档:

WordprocessingDocument^ wordDoc  = WordprocessingDocument::Create("C:\\...\MyFile.docx", WordprocessingDocumentType::Document);

MainDocumentPart^ mainPart = wordDoc->AddMainDocumentPart();

Document^ elt = gcnew Document( gcnew Body( gcnew Paragraph( gcnew Run( gcnew DocumentFormat::OpenXml::Wordprocessing::Text("Hello !") ) ) ) ); 

elt->Save(mainPart);

mainPart->Document->Save();

wordDoc->Close();

文件已创建,但为空。

如果我用 C# 编写此代码,文件很好,并且包含文本

为什么我的代码在 C++/cli 中创建的文件 (MyFile.docx) 没有文本?

【问题讨论】:

    标签: ms-word c++-cli openxml openxml-sdk xdoc


    【解决方案1】:

    我重现了这个问题。不确定发生了什么,此语法已记录为有效。当您使用 AppendChild() 方法显式编写代码时,它可以正常工作。像这样:

    WordprocessingDocument^ wordDoc  = WordprocessingDocument::Create("C:\\temp\\MyFile.docx", WordprocessingDocumentType::Document);
    MainDocumentPart^ mainPart = wordDoc->AddMainDocumentPart();
    
    mainPart->Document = gcnew Document;
    Body^ body = mainPart->Document->AppendChild(gcnew Body);
    Paragraph^ para = body->AppendChild(gcnew Paragraph);
    Run^ run = para->AppendChild(gcnew Run);
    run->AppendChild(gcnew DocumentFormat::OpenXml::Wordprocessing::Text("Hello !"));
    
    delete wordDoc;
    

    【讨论】:

    • 此代码有效!我不明白为什么第一个代码不起作用。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2017-09-15
    相关资源
    最近更新 更多