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

时间:2011-05-05 15:26:56

标签: c++-cli openxml-sdk

我正在尝试用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文件,但我无法打开创建的文件,因为它已损坏。请你帮我解决这个问题

提前致谢

1 个答案:

答案 0 :(得分:0)

尝试使用局部变量而不是从属性中读回。 e.g。

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

//...

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