我有一个可用的 ASP.NET MVC Web 应用程序来管理项目和客户。现在我想为一些客户生成一个word文件。在此文件中应显示有关客户的一些数据。每个生成的文件都应该具有相同的数据和相同的设计。因此,我想创建一个包含字段的新 Word 模板,并希望以编程方式填充占位符。
我的问题是我找不到明确的方法来做到这一点。有人知道好的学习资源吗?
I have a working ASP.NET MVC web application to manage projects and customers. Now I want to generate a word file for some customers. In this file should be displayed some data about the customer. Every generated file should have the same data and the same design. So I want to craete a new Word Template with the fields and want to fill the placeholders programmatically.
My problem is that I couldn't find a clear way to do that. Does anybody know a good learning resources?
发布评论
评论(4)
尝试此页面:
构建 Office Open XML 文件
打开 XML 文件 (docx)是包含 XML 文件的 ZIP 包。对于您的情况,我将创建原始模板的副本,然后使用 System.IO.Packaging API 打开文件并修改它。通过打开正确的 XML 文件并替换 XML 中的某些占位符,您应该能够获得您想要的结果。
Try this page:
Building Office Open XML Files
Open XML files (docx) are ZIP packages containing XML files. In your case, I would create a copy of your original template, then use the
System.IO.Packaging
API to open the file and modify it. By opening, the correct XML file and replacing certain placeholders in XML, you should be able to achieve the result you want.中创建/编辑 DOC 或 DOCX 的库
While trying to do the same I have found some libraries to create/edit DOC or DOCX in .Net
对于从模板文件(.dot)生成新文档应该非常容易,我认为这是您在打开的word应用程序文件中指定的参数,当您传递.dot文件的路径时,告诉word创建一个基于该文件的新 .doc,而不是编辑实际的模板文档。
对于表单字段和书签填充,有很多关于如何从 C# 执行此操作的在线示例,请参见此处:
MS Word Office Automation - 填写文本表单字段和复选框表单字段和邮件合并
for the new document generation from a template file (.dot) should be very easy, I think it's a parameter you specify in the word application file open or so, when you pass the path of the .dot file, telling word to create a new .doc based on that file and not edit the actual template document.
for form fields and bookmarks filling, lots of examples online on how to do it from C#, see here:
MS Word Office Automation - Filling Text Form Fields And Check Box Form Fields And Mail Merge
我遇到了同样的问题,找不到适合我们软件的模板引擎。这就是我开始这个开源项目的原因。
DocxTemplater 免费,开源
I encountered the same problem and couldn't find a suitable template engine for our software. That's why I started this open-source project.
DocxTemplater Free, OpenSource