Click here to Skip to main content
Click here to Skip to main content

Reporting: Embed dotnet code in MS Word files

, 15 Jan 2009
Rate this:
Please Sign up or sign in to vote.
Simple reporting engine using MS Word files as templates

Introduction

This is a simple reporting library. It allows developer to prepare report templates from MS word files. In MS word file, developer can embed vb.net code directly.  

Background 

I got the idea for this library from excellent Ruby Documatic project. 

Using the code 

There are two steps to create your reports from MS Word templates. First open new MS Word document and edit it. Use ### for code placeholder and $$$ for display placeholder. Code between begining ### and ending ### must be placed in the same paragraph. $$$ should be one-liner. I got an example of small template below. Then save document as MS Word XML document. You have to use MS Word 2007. 

s1.PNG

So let's review what's going on. There are some import statements on the top of the page.
Then there is a first table that is going to display 10 rows, right?
And second table is looping throug some test animal collection. You might observe that there is "data" variable. This variable is useful for sending your specific data into report. In Word template, just cast it to whatever type necessary and use it. 

The second step involves calling report:

      Dim data As New List(Of Object)
      data.Add(New Animal("Elephant", 1000))
      data.Add(New Animal("Tiger", 300))
      data.Add(New Animal("Crocodile", 250))
      data.Add(New Animal("Dog", 40))
      data.Add(New Animal("Chicken", 1))
      data.Add(New Animal("Dragon", 10000))
      Dim nd As New NDocs.Generator
      nd.References.Add("WindowsApplication2.exe")
      nd.Generate("xmlfile2.xml", "final2.xml", data)

This is final result: 

s2.PNG

Easy, right? Well, this library is still in very alpha phase, so it is possible that not all templates will work. Just keep in mind that you should put whole code between ### in one paragraph (use shift-enter), and $$$ are one-liners.  

How does this library works?

Actually, quite simple. It is 8 KB after all.

So here are the principles: 

1. Developer edits MS Word file (or later customer modifies it, or even customer sends first version and developers add tags) 

2. The MS Word file is saved as XML document  

3. Generation of final (output) file goes like this: 

3.1 XML parser extract code fragments (###) and original MS Word tags 

3.2 all tags are combined into dynamically generated source, which is then run-time compiled.. The result is new run-time, in-memory type (class), which can be instantiated and invoked.

3.3 this new type is then invoked via reflection 

3.4 the result is simply written to output file 

3.5 no MS Word APIs are needed


I am looking forward for your suggestions/opinions.    

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ivan.bolcina
Technical Lead Apida d.o.o.
Slovenia Slovenia
I am a programmer.

Comments and Discussions

 
GeneralSomething new Pinmemberashu fouzdar22-Jan-09 21:42 
GeneralMy vote of 1 PinmvpDave Kreskowiak15-Jan-09 3:39 
GeneralRe: My vote of 1 Pinmemberivan.bolcina15-Jan-09 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 15 Jan 2009
Article Copyright 2009 by ivan.bolcina
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid