How to generate report in word 2010 using c# , dot net, word templates, xml

learning picture learning · Jan 18, 2013 · Viewed 17.7k times · Source

We have a database application that stores data that we want to report in Microsoft Word.

Suppose all information of my customers is stored on a database system and I am now requested to create hundreds of word letters, reports that will be sent to my customers. These letters have the same content but different customer name, customer address, etc.

I want to make use of Office Word 2010 by creating document template with content controls using c# and .Net, sql as database to replace the content of this template.

I've been looking for articles on automating Word 2010 in C# and dot net and sql. Could someone give me a push in the right direction?

Answer

ninten picture ninten · Jan 21, 2013

You can use Interop.Word in your program, but keep in mind that the available documentation is very scarce. I managed to develop my application looking at examples like this one from C-SharpCorner or this one from WindowsDevCenter. Even if the examples are old, you can get the main idea and get familiar with the syntax, and write your program afterwards with an updated version of Interop.Word (which has a slightly simpler syntax).

In your case, you should create a neat Word template, with bookmarks located in the places of your document where you will insert the customer information. Then you can open the template from your program and navigate it using those bookmarks, as you insert the information retrieved from your database.

There are other interesting alternatives to Interop.Word that you could try if you don't want to go too deep into Word automation, such as DocX (which doesn't even require Microsoft Word or Office to be installed) or Open XML (to generate .docx files).