[英]C# Populate word template with OpenXml
我想创建一个新的Word文档,该文档基于带有内容控件的模板。
我需要填充那些内容控件(文本)。
我只找到了如何生成新的Word文档,却没有基于模板。
您有任何链接或教程吗?
也许我使用OpenXml填写模板不对吗?
// Create a Wordprocessing document.
using (WordprocessingDocument myDoc =
WordprocessingDocument.Create("d:/dev/test.docx",
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是正确的方法,因为您无需安装MS Word就可以处理文档-考虑服务器场景。 但是,它的学习曲线陡峭而乏味。 以我的拙见,最好的方法是要求预算来购买第三方工具包,并专注于业务领域而不是OpenXML调整。
在您的示例中,您在Word中有一个模板,想要用数据更新(合并)它。 对于某些场景,我已经使用Docentric Toolkit完成了,并且效果很好。 一旦了解了其工作原理,便可以快速创建解决方案。 如果您在DT遇到麻烦,伙计们不会让您失望的。 请参阅此链接( http://www.codeproject.com/Articles/759408/Creating-Word-documents-in-Net-using-Docentric-Too ),以了解如何使用它。 默认情况下,它创建.docx,但是您也可以获取固定的最终文档(pdf或xps)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.