Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Reporting: Embed dotnet code in MS Word files

0.00/5 (No votes)
15 Jan 2009 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here