MS Word 邮件合并类似功能,允许从 ASP.Net Web 应用程序导出到 Word 文档
我有一个要求,我需要允许用户上传带有可在数据库中找到的某些字段的占位符的 Word 文档。这将是他们的模板。例如,占位符可能会以 @@ 或其他内容开头。例如,
Dear @@Title @@Lastname
他们然后可以抓取一条记录并点击导出到 Word 文档。然后他们将选择模板。他们可以选择模板,然后单击继续。然后,我将获取模板并将 @@Title
替换为数据库中所选记录的标题字段。我不确定从哪里开始或需要哪些组件来执行此操作。
从我最初的调查来看,我似乎可以使用 Office 2007 的新开放 XML 标准来完成此操作。因此,也许我应该读取模板并将所有内容保存到某个数据库表中。然后,当用户想要导出时,我再次获取内容,然后搜索并替换 @@
占位符并正确链接它们。然后再次将文档保存到输出流,然后在浏览器上弹出保存对话框。
我正在使用 ASP.Net MVC 并且处于托管环境中。我也可能正在考虑动态创建新的视图类型,并在用户上传模板时动态创建新视图。但不确定这种方法是否有效。
这是一个好方法吗? 我应该看什么工具? 还有其他建议吗?
I have requirement where I need to allow users to upload a Word document with place holders for certain fields which can be found in the database. This will be their template. For example the place holders might be prepended with @@ or something. For example
Dear @@Title @@Lastname
They then can grab a record and hit export to Word document. This will then let them choose the template. They can select the template and then click continue. I will then get the template and replace the @@Title
with the title field in the database for the selected record. I am not sure where to start or what components I need to do this.
From my initial investigation it seems that I can do this with the new open XML standard for Office 2007. So perhaps I should read in the template and save all the contents to a db table somewhere. Then when the use wants to export I get the contents again and then do a search and replace for the @@
placeholders and link them properly. Then save the document to the output stream again which will then bring up the save dialog on their browser.
I am using ASP.Net MVC and am in a hosted environment. I was also maybe contemplating dynamically creating a new View type and dynamically creating new views when the user uploads a template. Not sure that this approach will work though.
Is this a good approach?
What tools should I be looking at?
Any other suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这类似于我们将数据插入到 Word 文档中然后将其返回给用户的方法。我们打开 .docx 文件(它是一个 zip 文件,很容易提取),提取文档(在名为 document.xml 的 word 文件夹中),进行替换,然后将文档放回到 .docx 文件中并将其返回给用户。
我们遇到的一个问题是单词在奇怪的地方插入了标签,尤其是拼写/语法错误之类的事情,所以我们在进行搜索/替换时需要小心。
我们决定不将文档中的字段存储在数据库中,以便轻松更新文档。
我们使用 dotnetzip 组件来打开 .docx 文件
我们还做了一些事情,将多个文档合并为一个大文档文档以节省下载次数。如果我记得我们使用 open xml 工具包 来进行此合并。该网站还提供了大量其他可能有用的信息。
This is similar to an approach we took for inserting data into word documents and then returning them to the user. We opened the .docx file (it's a zip file so easy to extract) extracted the document (in the word folder called document.xml), did the replace and then put the document back into the .docx file and returned it to the user.
An issue we hit were that word inserted tags at strange places, especially things like spell/grammar errors, so we needed to be careful when we did the search/replace.
We decided not to store the fields from the document in a database to allow the documents to be easily updated.
We used dotnetzip component for opening the .docx files
Something we also did was to combine several documents into a single large document to save on the number of downloads. If I remember we used the open xml toolkit to do this merging. The website has also got loads of other information that may be of use.
请查看 Scott Guthries 博客文章,了解微软很快就会推出代号为“Razor”的新视图引擎。在评论中,有人谈论它能够在邮件合并场景中使用,就像您谈论 ASP.NET MVC 视图一样。
Check out Scott Guthries blog post about the new view engine code named "Razor" coming out real shortly from Microsoft. In the comments there is talk about it being able to be used in mail merge scenarios like you talk about with ASP.NET MVC views.