开发者

OpenXML SDK -Converting C# to C++/CLI

开发者 https://www.devze.com 2023-03-02 23:14 出处:网络
I\'ve got C# code for creating a document, I want to write the same in C++/CLI. private void HelloWorld(string documentFileName)

I've got C# code for creating a document, I want to write the same in C++/CLI.

private void HelloWorld(string documentFileName) 
{
    // Create a Wordprocessing document. 
    using (WordprocessingDocument myDoc =
           WordprocessingDocument.Create(documentFileName, 
               WordprocessingDocumentType.Document)) 
    { 
        // Add a new main document part. 
        MainDocumentPart mainPart = myDoc.AddMainDocumentPart(); 
        //Create Document tree for simple document. 
        mainPart.Document = new Document(); 
        //Create Body (this element contains
        //other elements that we want to include 
        Body body = new Body(); 
        //Create paragraph 
        Paragraph paragraph = new Paragraph(); 
        Run run_paragraph = new Run(); 
        // we want to put that text into the output document 
        Text text_paragraph = new Text("Hello World!"); 
        //Append elem开发者_StackOverflow社区ents 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(); 
    } 
}

Also please suggest me any links where I can find C++/CLI example for OpenXML SDK


Here's a direct translation:

private:
    void HelloWorld(String^ documentFileName)
    {
        msclr::auto_handle<WordprocessingDocument> myDoc(
            WordprocessingDocument::Create(
                documentFileName, WordprocessingDocumentType::Document
            )
        );
        MainDocumentPart^ mainPart = myDoc->AddMainDocumentPart();
        mainPart->Document = gcnew Document;
        Body^ body = gcnew Body;
        Paragraph^ paragraph = gcnew Paragraph;
        Run^ run_paragraph = gcnew Run;
        Text^ text_paragraph = gcnew Text(L"Hello World!");
        run_paragraph->Append(text_paragraph);
        paragraph->Append(run_paragraph);
        body->Append(paragraph);
        mainPart->Document->Append(body);
        mainPart->Document->Save();
    }

msclr::auto_handle<> should generally be considered more idiomatic than try..finally, just as std::shared_ptr<> and std::unique_ptr<> are in C++.


I take it you have tried something? I don't have access to a compiler

http://en.wikipedia.org/wiki/C%2B%2B/CLI should get you started.

If you wonder about translating the using construct (good question, had you asked it!), I suggest something along the following lines (note the try {} finally { delete ... } idom)

private:

void HelloWorld(String^ documentFileName) 
{ 
    // Create a Wordprocessing document. 
    WordprocessingDocument ^myDoc = WordprocessingDocument::Create(documentFileName, WordprocessingDocumentType::Document);
    try
    {
        // Add a new main document part. 
        MainDocumentPart mainPart = myDoc::AddMainDocumentPart(); 
        //Create Document tree for simple document. 
        mainPart->Document = gcnew Document(); 
        //Create Body (this element contains
        //other elements that we want to include 
        Body body = gcnew Body(); 
        //Create paragraph 
        Paragraph paragraph = gcnew Paragraph(); 
        Run run_paragraph = gcnew Run(); 
        // we want to put that text into the output document 
        Text text_paragraph = gcnew 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(); 
    } finally
    {
        delete myDoc;
    }   
}

I want to repeat I have no compiler available at the moment, so it may be rough around the edges, but should provide some information nonetheless


OpenXML SDK C++ Examples


The syntax is basically the same. "new" needs to be replaced by gcnew, the "." by -> (e.g. body.Append(paragraph) will be body->Append(paragraph). The tricky part will be the "using" directive. To do it the C++ way, you need some kind of smart pointer that "deletes" the object at the end of the block (with delete meaning calling the IDisposable interface) - this is called RAII.

0

精彩评论

暂无评论...
验证码 换一张
取 消