C++ cli OpenXML SDK-将C转换为C++/CLI--创建的文件已损坏,无法打开

C++ cli OpenXML SDK-将C转换为C++/CLI--创建的文件已损坏,无法打开,c++-cli,openxml-sdk,C++ Cli,Openxml Sdk,我试图用C++/CLI编写一个简单的hello world应用程序来创建一个Word文档,我在下面发布了这个文档 int main(array<String^>^ args) { String^ documentFileName=L"Hello4.docx"; WordprocessingDocument ^myDoc = WordprocessingDocument::Create(documentFileName, WordprocessingDocumentTy

我试图用C++/CLI编写一个简单的hello world应用程序来创建一个Word文档,我在下面发布了这个文档

int main(array<String^>^ args)
{
    String^ documentFileName=L"Hello4.docx";
    WordprocessingDocument ^myDoc = WordprocessingDocument::Create(documentFileName, WordprocessingDocumentType::MacroEnabledDocument);
    MainDocumentPart^ mainPart = myDoc->AddMainDocumentPart();
    mainPart->Document  = gcnew Document();
    Body^ body = gcnew Body();
    Paragraph^ paragraph = gcnew Paragraph();
    Run^ run_paragraph = gcnew Run();
    DocumentFormat::OpenXml::Wordprocessing::Text^ text_paragraph = gcnew DocumentFormat::OpenXml::Wordprocessing::Text(L"Hello ..asdks");
    run_paragraph->Append(text_paragraph);
    paragraph->Append(run_paragraph);
    body->Append(paragraph);
    mainPart->Document->Append(body);
    mainPart->Document->Save();

    return 0;
}
上面的程序创建了hello.docx文件,但我无法打开创建的文件,因为它已损坏。您能帮我解决这个问题吗


提前感谢

尝试使用局部变量,而不是从属性中读取。e、 g

doc = gcnew Document();
mainPart->Document = doc;

//...

doc->Append(body);
doc->Save();

我试过上面的答案,它不起作用。您能提出其他解决方案吗?您收到的确切错误是什么?或者没有错误,但是主部件->文档->保存;什么也不产生?@Otaku-我没有在那一行得到任何错误,但是如果我运行上面的代码,Hello.docx文件正在创建,但我无法打开该文件。它说该文件已损坏,无法打开。这是连接到文件的WordprocessingDocument对象,而不是文档子对象。根据,您应该调用myDoc->Close;@本·沃格特-非常感谢。它现在正在工作。但是插入的文本在文档中丢失了,我现在来检查一下。无论如何,非常感谢。Ben Voigt-编译器没有解析行mainPart->document->Appendbody;mainPart->Document->Save;。请帮我摆脱这个困境。