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

18,043

Solution 1

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).

Solution 2

So for start DO NOT USE INTEROP, i've been using it for the last 4 years, and i have to tell u it's not a good idea (it's really lost, and u'll hit lots of problems. It's actually written on Microsoft's site that you shouldn't use it for server side generation.

You should use the OpenXML SDK, i've actually just started using it but i have to say that even if it seems a bit harder to use, it's definetly a lot faster that using interop and the best thing is that know i don't need any programs from the Office suite to be installed on my pc, the downside is that i can't export to PDF or XPS anymore without a 3rd party library

You can find the sdk here http://www.microsoft.com/en-us/download/details.aspx?id=30425

I suggest downloading the tool as well it's pretty useful.

This is a good tutorial to start with it really helped me a lot.

http://msdn.microsoft.com/en-us/library/office/bb448854.aspx

And u can also use the API documentation from the Productivity Tool which is on the same site as the sdk

Solution 3

Your answer is OpenXML SDK.

  1. goto Open XML SDK 2.5 for Microsoft Office
  2. download OpenXMLSDKV25 and OpenXMLSDKToolV25
  3. install sdk and productivity tool
  4. open word and create your template document
  5. open Open XML SDK Productivity Tool
  6. open word document
  7. right click on document name from Document Explorer and select reflect code
  8. copy all generated code to your project
  9. find and edit "Paragraph" or "Run" that you want replace with your data

Solution 4

I've used the Office.Interop assemblies in the past for this kind of functionality but this method carries a few distinct disadvantages:

  • Word must be installed on the machine where the code is running
  • The Interop assemblies actually start up Word in the background, so you have to be careful to dispose of everything properly and handle errors, otherwise you'll end up with Word processes wasting CPU/Memory on the host server
  • The APIs are not very pleasant to work with and documentation is somewhat scarce

I've also played with DocX and Open XML, both of which have their merits but tend to be slightly limited by comparison with Interop. My advice would be to attempt the functionality using DocX or Open XML and only fall back to Interop if you can't achieve the functionality any other way. There should be plenty of tutorials online for all three APIs.

Solution 5

Microsoft recommends OpenXml for any application running in a server process, and this approach would probably be one of the best for reducing dependencies (as others have mentioned). Here are some links to get you started:

Download OpenXml SDK 2.0: http://www.microsoft.com/en-au/download/details.aspx?id=5124

Useful Tutorials: http://msdn.microsoft.com/en-us/library/ff478255.aspx

Share:
18,043
learning
Author by

learning

Updated on June 09, 2022

Comments

  • learning
    learning almost 2 years

    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?

  • learning
    learning over 11 years
    Is there the possibility to build customize xml and binding to data source with DocX?
  • Henry Wilson
    Henry Wilson over 11 years
    I'm not sure I follow, can you elaborate as to what you mean?
  • Henry Wilson
    Henry Wilson about 11 years
    This post could be improved greatly by a bit of attention to spelling and grammer and less use of text-speak shorthand.