【问题标题】:C# Populate word template with OpenXmlC# 使用 OpenXml 填充 word 模板
【发布时间】:2015-04-07 09:47:34
【问题描述】:

我想创建一个基于带有内容控件的模板的新 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();
        } 

【问题讨论】:

    标签: c# ms-word openxml


    【解决方案1】:

    使用 OpenXML 是正确的方法,因为您无需安装 MS Word 即可操作文档——想想服务器场景。然而,它的学习曲线陡峭而乏味。以我的拙见,最好的方法是要求预算购买 3rd 方工具包并专注于业务领域而不是 OpenXML 调整。

    在您的示例中,您在 Word 中有一个模板,并希望用数据更新(合并)它。对于我们的一些场景,我已经使用Docentric Toolkit 完成了此操作,并且效果很好。一旦您了解了它的工作原理,您就可以快速创建解决方案。如果您在 DT 遇到困难,他们不会让您失望。请参阅此链接 (http://www.codeproject.com/Articles/759408/Creating-Word-documents-in-Net-using-Docentric-Too) 了解如何使用它。默认情况下,它会创建 .docx,但您也可以获得固定的最终文档(pdf 或 xps)。

    【讨论】:

      猜你喜欢
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      相关资源
      最近更新 更多