简体   繁体   English

使用C#从现有模板创建Word文档

[英]Using C# to create a Word document from an existing template

I have several Word templates and I wish to use these to dynamically create Word documents in my app. 我有几个Word模板,我希望使用它们在我的应用程序中动态创建Word文档。 I wish to avoid using automation at all costs as this is no good. 我希望避免不惜一切代价使用自动化,因为这不好。 I know that I can use both HTML and XML to create word documents but I just don't know where to start with regards to using a template that may well have images in the footer or the header of a document. 我知道我可以同时使用HTML和XML来创建Word文档,但是我不知道从哪里开始使用模板,该模板可能在文档的页脚或页眉中就有图像。

I use the OpenXML SDK with Word 2007. After you get the hang of it, it's not so bad. 我将OpenXML SDK与Word 2007结合使用。掌握了这些窍门之后,还不错。 I have several template docx files that I scan through to search and replace for placeholder strings with what I want, and then can stitch together multiple templates into one document if I want to. 我扫描了几个模板docx文件,以搜索并替换为所需的占位符字符串,然后根据需要将多个模板缝合到一个文档中。 It's nice because I can start with docx files as the template and modify them while the whole time staying within the realm of the docx format. 很好,因为我可以从docx文件开始作为模板,然后在整个时间都在docx格式范围内的同时对其进行修改。 If an image is in the docx when you start modifying it, it'll be there after you re-save it after modification (provided you didn't programmatically remove it of course). 如果图像在您开始修改时在docx中,则在修改后重新保存它之后它就会在那里(前提是您当然没有以编程方式删除它)。

If you have more details with what you'll be doing, let us know. 如果您要执行的操作有更多详细信息,请告诉我们。

You could use DocX . 您可以使用DocX It's free, very easy to use, with nice tutorials and is feature reach. 它是免费的,非常易于使用,具有精美的教程,并且功能广泛。 It works with only DOCX documents thou. 它仅适用于DOCX文档。 Also development is currently on hold until the author will finish his semester. 在作者完成其学期之前,开发也处于暂停状态。 Here's detailed blog about it. 这里是有关它详细博客。

It has good example of using template in his Invoice Example . 在其“ 发票示例”中有使用模板的很好示例。

MigraDoc http://www.pdfsharp.net/MigraDocOverview.ashx is a free utility for exporting PDF/Word/HTML files. MigraDoc http://www.pdfsharp.net/MigraDocOverview.ashx是用于导出PDF / Word / HTML文件的免费实用程序。 I've not worked with it using templates as yet however, you could use the DDL files to persists a layout for your files to be re-used. 我还没有使用模板来使用它,但是,您可以使用DDL文件来保留要重新使用的文件的布局。

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

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