Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 942
learning Avatar asked Jan 18 '13 06:01

learning


People also ask

How do I write C code in Word?

Go to the Insert tab, click the Object drop-down arrow, and pick “Object.” On the Create New tab, select “OpenDocument Text” as the Object Type. Click “OK.” A new Word document will open for you to insert your code or command.

How do you copy a Word in C?

Go to the original Word document that needs to be copied, select all its content, and press Ctrl + C. That will copy the content, which you can easily paste into another Word file by pressing Ctrl + V.


2 Answers

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

like image 144
ninten Avatar answered Oct 13 '22 16:10

ninten


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.

like image 1
Henry Wilson Avatar answered Oct 13 '22 17:10

Henry Wilson