如何从数据值创建Word文档 [英] How to create a Word document from data values

查看:140
本文介绍了如何从数据值创建Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要从ASP.NET应用程序中预先设计的单词模板自动生成文档。该解决方案应该使我们能够使用数据库数据填充(Word)模板文档。文档应该是Word文档的形式,以便最终用户能够在生成后添加/修改它们。



是否存在.NET用应用程序数据执行word文档/模板(邮件合并)的填充的库?我们倾向于使用免费图书馆,但也可以听到商业图书馆。





更新:

如果其他人在将来遇到同样的问题,我们最终会使用Docentric Toolkit。

We need to automate the generation of documents from pre-designed word templates in ASP.NET application. The solution should enable us to populate (Word) template documents with the database data. The documents should be in the form of Word documents so the end users will be able to add/modify them after they have been generated.

Is there an existing .NET library that performs population of word documents/templates (mail merge) with application data? We are inclined to use a free library but are also open to hear about the commercial ones.


UPDATE:
In case anyone else having the same problem in the future, we ended up using Docentric Toolkit.

推荐答案

这取决于Word(r)的版本你我想要瞄准。对于旧的.DOC文件,Microsoft具有强大的互操作程序集,但它需要驻留在运行任务的计算机/服务器上。这可能会给面向公众的机器带来安全问题。



对于新的.DOCX文件,我能够通过创建一个text =占位符的模板来完成任务要插入的数据。我将模板重命名为.ZIP文件。 (新的.docx格式只是一个包含特定文档元素的zip文件(文件夹结构,xml文件等)。



在zip文件夹中有一个文件〜 \ word\document.xml包含文档的文本。



对于每个新文档,我将.zip文件解压缩到一个空目录,替换使用正确的数据在xml文件中占位符,然后重新压缩目录并将生成的zip文件重命名为.docx结尾。最后一步是删除临时文件,然后提供下载新.docx文件的链接。



我使用免费的Ionic拉链库来实现ASP http://dotnetzip.codeplex .com / [ ^ ]解压



我也成功地交换图像(在〜/ word / media文件夹中),文档属性等等。



此方法非常有效适用于只有少量图像和几页文本的简单文档。对于包含许多章节,标题,许多大图像,复杂格式,目录的非常大的文档,它可能会在un-zip,re-zip阶段使服务器紧张。
It depends on the version of Word(r) you're wanting to target. For the old .DOC files, Microsoft has a robust interop assembly, but it needs to reside on the computer/server running the tasks. This may present security concerns for public-facing machines.

For the new .DOCX files, I was able to accomplish the task by creating a template with text=placeholders for the data to be inserted. I renamed the template to be a .ZIP file. (The new .docx format is simply a zip file with specific document elements (folder structure, xml files, etc).

In the zip folders there is a file ~\word\document.xml that contains the text of the document.

For each new document, I unzip the .zip file to an empty directory, replace the placeholders in the xml file with the proper data, and then re-zip the directory and rename the resulting zip file to end in .docx. The last step is to delete the temporary files and then provide a link for downloading the new .docx file.

I use the free Ionic zipping library for ASP http://dotnetzip.codeplex.com/[^] to unzip

I've also had success with swapping out images (in the ~/word/media folder), document properties, and so forth.

This method works very well for simple documents with only a few images and a few pages of text. For very large documents with many chapters, headings, many large images, complex formatting, table of contents, it can strain the server during the un-zip, re-zip phases.


有一个看这里:

如何使用Word Mail Merge和宏导入数据 [ ^ ] br />
Word 2013和文本文件作为邮件合并数据源 [ ^ ]

Word文档中的邮件合并 [ ^ ]
Have a look here:
How to use Word Mail Merge and Macros to Import Data[^]
Word 2013 and text files as mail merge data sources[^]
Mail merge in Word document[^]


通常,.NET开发人员在实现文档生成自动化或邮件合并(在您的情况下)时有几个选项可供选择:



  • Office Word Automation(Office COM):虽然这种方法仍然可以在.NET中使用,但它是不推荐,特别是在服务器端(它依赖于MS Word,需要在生产机器上安装)。
  • Open XML SDK:This < a href =http://msdn.microsoft.com/en-us/library/office/bb448854(v=office.15).aspx>微软的图书馆 [ ^ ]可用于创建自己的文档解析功能以处理word文档(查找和替换占位符...)。这是通过Office COM处理文档的首选方法,因为它不需要在服务器上安装Word。
  • 第三方库:据我所知,有几个.NET库可以让您处理文档不依赖于MS Word。在我的公司,我们使用 Docentric Toolkit ,它具有非常好的邮件合并功能。
In general, .NET developers have several options to choose from when implementing document generation automation or mail merge (in your case):

  • Office Word Automation (Office COM): Although this approach can still be used in .NET, it is not recommended any more, especially on server side (it relies on MS Word, which needs to be installed on the production machine).
  • Open XML SDK: This Microsoft's library[^] can be used to create your own document parsing functionality to process word documents (to find and replace placeholders...). This is the preferred way to process documents over the Office COM because it does not need Word installed on the server.
  • 3rd party libraries: As far as I know there are several .NET libraries that enable you to process documents without relying on MS Word. In my company we are using Docentric Toolkit which has really good mail merge capabilities.


这篇关于如何从数据值创建Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆