我有用于创建文档的 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();
}
}
另外,请建议我可以找到 OpenXML SDK 的 C++/CLI 示例的任何链接
最佳答案
这里是直接翻译:
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++ 中。
关于c# - OpenXML SDK - 将 C# 转换为 C++/CLI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5866048/