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

8

6

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?

Granulose answered 18/1, 2013 at 6:52 Comment(6)
See #7383204. It's not supported to use office Automation in an ASP.NET application - and usually doesn't work.Ailssa
I used to work with Interop.Word namespace to automate such process. See codeproject.com/Articles/18703/Word-2007-AutomationBosom
The question isn't tagged ASP.NET and doesn't mention it in the body.Censurable
Doesn't that scream "Use Word's serial letter function!"?Koa
Check this project. flexdoc.codeplex.com with flexdoc you don't need to install ms word. I used flexdoc for creating word file.Segarra
This article contains downloadable source code which can be readily used to solve your problem.Anaximenes
O
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).

Obsessive answered 21/1, 2013 at 11:12 Comment(0)
B
1

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.

Bickford answered 21/1, 2013 at 16:29 Comment(2)
Is there the possibility to build customize xml and binding to data source with DocX?Granulose
I'm not sure I follow, can you elaborate as to what you mean?Bickford
U
1

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

Undertaker answered 25/1, 2013 at 3:21 Comment(0)
P
1

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

Piazza answered 25/1, 2013 at 13:28 Comment(1)
This post could be improved greatly by a bit of attention to spelling and grammer and less use of text-speak shorthand.Bickford
A
1

I agree with others that the OpenXML SDK is a good way to go for the same reasons. I am in the midst of creating a similar kind of report generation as you are. The reports I need to generate unfortunately are very dynamic not only with data but with the layout.

If your layout does not need to change then I would strongly suggest using the SDK Tool, it lets you take any word document and generate the c# code for you to recreate the same exact doc. From that point all you have to do is replace the text with the data you want in the code. You could make a generic report in word, get the code with the tool, then just do a search for the placeholder text in the code and replace that string with a variable. its as easy as text1.text = reportData;

I also find the SDK Tool a great way to learn the code, you can compare two files side by side and see how they differ in the code.

Architrave answered 25/1, 2013 at 13:50 Comment(0)
P
1

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
Punkah answered 28/1, 2013 at 8:59 Comment(0)
W
0

I have used both interop and syncfusion to create reports (although I was generating an excel report but both technologies can be used to generate word reports also). My feedback :-

  1. Although interop is included in most computers which have visual studio (not sure about non visual studio computers), usage of interop requires the presence of compatible microsoft excel also on the computer generating the report whereas syncfusion does not requires the presence of microsoft excel on the same computer.
  2. If you plan to use asp.net, then interop is not supported (atleast it was not when I worked) whereas syncfusion is supported.

Link for syncfusion :- http://www.syncfusion.com/products/aspnet/docio

Summary :- If you are developing a console application and server(where the application will be deployed) has microsoft office installed, then interop might be a good idea. Otherwise, you can have a look at syncfusion also.

Weak answered 23/1, 2013 at 17:31 Comment(0)
C
0

You can usee Free .NET Word API to generate report in word 97 ~ 2010 using c#.

        //Create word document
        Document document = new Document();
        document.LoadFromFile(@"..\..\..\..\..\..\Data\Fax.doc");

        string[] filedNames = new string[]{"Contact Name","Fax","Date"};

        string[] filedValues = new string[]{"John Smith","+1 (69) 123456",System.DateTime.Now.Date.ToString()};

        document.MailMerge.Execute(filedNames, filedValues);

        //Save doc file.
        document.SaveToFile("Sample.doc", FileFormat.Doc);
Chetchetah answered 11/4, 2014 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.