简体   繁体   English

从模板构建Word文档

[英]Build Word Document from template

I have a request to create a word document on the fly based on a template provided to me. 我有一个请求,根据提供给我的模板动态创建word文档。 I have done some research and everything seems to point at OpenXML. 我做了一些研究,一切似乎都指向OpenXML。 I have looked into that, but the cs file that gets created is over 15k lines and is breaking my VS 2010 (causing it to not respond every time I make a change). 我已经研究过了,但是创建的cs文件超过15k行并且正在破坏我的VS 2010(导致它每次进行更改时都不响应)。

I have been looking at this tutorial series on Open XML http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2011/10/13/getting-started-with-open-xml-development.aspx 我一直在看这个关于Open XML的教程系列http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2011/10/13/getting-started-with-open-xml-development.aspx

I have done things in the past with text files and Regular Expressions, but since Word encrypts everything, that does not work. 我以前用文本文件和正则表达式做过一些事情,但是由于Word加密所有内容,所以不起作用。 Are there any other options that are fairly lightweight for creating word documents from templates. 是否有任何其他选项相当轻量级从模板创建word文档。

//Hi, It is quite simple. //嗨,很简单

            //First, you should copy your Template file into another location.
            string SourcePath = "C:\\MyTemplate.dotx";
            string DestPath = "C:\\MyDocument.docx";
            System.IO.File.Copy(SourcePath, DestPath);

            //After copying the file, you can open a WordprocessingDocument using your Destination Path.

            WordprocessingDocument Mydoc = WordprocessingDocument.Open(DestPath, true);

            //After openning your document, you can change type of your document after adding additional parts into your document.
            mydoc.ChangeDocumentType(WordprocessingDocumentType.Document);

            //If you wish, you can edit your document 
            AttachedTemplate attachedTemplate1 = new AttachedTemplate() { Id = "MyRelationID" };

            MainDocumentPart mainPart = mydoc.MainDocumentPart;
            MySettingsPart = mainPart.DocumentSettingsPart;

            MySettingsPart.Settings.Append(attachedTemplate1);

            MySettingsPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", new Uri(CopyPath, UriKind.Absolute), "MyRelationID");



            //Finally you can save your document.
            mainPart.Document.Save();

I am currently working on something along these lines and I have been making use of the Open XML SDK and the OpenXmlPowerTools The approach been taken is taking the actual template file opening it up and putting text into various place holders within the template document. 我目前正在研究这些方面的内容,并且我一直在使用Open XML SDKOpenXmlPowerTools 。采用的方法是将实际模板文件打开并将文本放入模板文档中的各个占位符。 I have been using content controls as the place markers. 我一直在使用内容控件作为位置标记。

The SDK tool to open up a document has been invaluable in being able to compare documents and see how it is constructed. 用于打开文档的SDK工具在比较文档和查看文档构造方面具有无可估量的价值。 However the code generated from the tool I have been refactoring heavily and removing sections that are not being used at all. 但是,从工具生成的代码我已经大量重构并删除了根本没有使用的部分。

I can't talk about doc files but with docx files they are not encrypted they are just zip files that contain xml files 我不能谈论doc文件,但是对于docx文件,它们没有加密,它们只是包含xml文件的zip文件

Eric White's blog has a large number of examples and code samples which have been very useful Eric White的博客中有大量的示例和代码示例非常有用

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

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