简体   繁体   English

C#使用OpenXml填充Word模板

[英]C# Populate word template with OpenXml

I would like to create a new Word document which is based on a template with content controls. 我想创建一个新的Word文档,该文档基于带有内容控件的模板。

I need to fill those contents controls (text). 我需要填充那些内容控件(文本)。

I only found how to generate a new Word document but not bases on a template. 我只找到了如何生成新的Word文档,却没有基于模板。

Do you have any link or tutorial ? 您有任何链接或教程吗?

Maybe am I wrong using OpenXml to fill a template ? 也许我使用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();
        } 

Going OpenXML is the right approach, because you can manipulate documents without the need to have MS Word installed – think server scenarios. 使用OpenXML是正确的方法,因为您无需安装MS Word就可以处理文档-考虑服务器场景。 However, its learning curve is steep and tedious. 但是,它的学习曲线陡峭而乏味。 In my humble opinion, the best way is to ask for a budget to purchase a 3rd party toolkit and focus on the business domain and not on OpenXML tweaks. 以我的拙见,最好的方法是要求预算来购买第三方工具包,并专注于业务领域而不是OpenXML调整。

In your example, you have a template in Word and want to update (merge) it with data. 在您的示例中,您在Word中有一个模板,想要用数据更新(合并)它。 I have done this with Docentric Toolkit for some of our scenarios and it works very well. 对于某些场景,我已经使用Docentric Toolkit完成了,并且效果很好。 Once you understand the basics of how it works you create solutions quickly. 一旦了解了其工作原理,便可以快速创建解决方案。 If you get stuck guys at DT won't let you down. 如果您在DT遇到麻烦,伙计们不会让您失望的。 See this link ( http://www.codeproject.com/Articles/759408/Creating-Word-documents-in-Net-using-Docentric-Too ) to get the idea on how you can use it. 请参阅此链接( http://www.codeproject.com/Articles/759408/Creating-Word-documents-in-Net-using-Docentric-Too ),以了解如何使用它。 By default it creates .docx, but you can get fixed final documents (pdf or xps) as well. 默认情况下,它创建.docx,但是您也可以获取固定的最终文档(pdf或xps)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM