Word Template, populate fields, C# .NET

2019-05-29 08:52发布

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条回答
beautiful°
2楼-- · 2019-05-29 09:02

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.

查看更多
登录 后发表回答