Word Template, populate fields, C# .NET

旧时模样 提交于 2019-12-10 10:37:36

问题


I have a word document (docx format) that I need to populate with data from a dictionary . The key of the dict will be the word field to be populated. I was wondering what is the best way to do this (bookmarks, mail merge fields, etc)? I did a little work with Bookmarks, but I would like to be able to reuse some of the fields (like "first_name" field etc), bookmarks are unique (or I seem to think so).
A few lines of code on how to do this would be really helpful. (c# .net 4.0)


回答1:


You can make xpath data bindings in a .docx document. A .docx file is just a zipped file in the Office Open XML which can be opened in .Net without third-party libraries.

http://msdn.microsoft.com/en-us/library/system.io.packaging.package.open.aspx

And you can then supply the .docx file with a XML file in your own format. Then the only thing you have to do is to edit the embedded XML file and not the document itself. See:

http://blogs.msdn.com/b/mikeormond/archive/2008/06/20/word-2007-content-controls-databinding-and-schema-validation.aspx

http://blogs.msdn.com/b/acoat/archive/2007/03/01/linking-word-2007-content-controls-to-custom-xml.aspx

And a custom tool to do the bindings:

http://dbe.codeplex.com/

You could properly also use some .Net interaction with Word itself but I can't help you with that.



来源:https://stackoverflow.com/questions/3493856/word-template-populate-fields-c-sharp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!