我有用于创建文档的C#代码,我想用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 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();
}
}此外,请建议我任何链接,我可以找到C++/CLI示例的OpenXML软件开发工具包
发布于 2011-05-03 15:03:43
这是一个直接的翻译:
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<>比try..finally更惯用,就像std::shared_ptr<>和std::unique_ptr<>在C++中一样。
发布于 2011-05-03 15:01:52
我想你已经尝试过什么了吧?我不能访问编译器
http://en.wikipedia.org/wiki/C%2B%2B/CLI应该可以帮助您入门。
如果您想知道如何翻译using结构(这是个好问题,如果您已经问过了!),我建议您使用以下内容(请注意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;
}
}我想重复一遍,我目前没有可用的编译器,所以它可能是粗糙的边缘,但仍然应该提供一些信息
发布于 2011-05-03 15:02:00
https://stackoverflow.com/questions/5866048
复制相似问题