Generating reports with Templater, MS Word & C#

Whenever developers think of report design or development, they actually think of SSRS or other report designers/engines. But many times it happens like just for the sake of few basic reports, customers will not be ready to buy those expensive reporting tools and also it is obvious that using such tools are meaningless for a small requirement.

You can create reports with MS Word and MS Excel easily, it was not so easy to push data into it programmatically. In this post, let us look into a small reporting library known as "Templater" to push data into reports. Well ,
it is not free :( but it's not too expensive (Starts from 99$). 

A trial version of this library is available but adds a odd message! but the library is sweet :) and exactly does what it says.

The biggest advantage is that, we can create report templates using Microsoft Office Word or Excel (Mostly everyone would have this installed). Then you can just add tags (something like [[Name comes Here]] ) in the document which you can then replace with actual data at runtime through your C# / VB.Net code.

Let us create a small Proof of concept to see it working. You can create reports from Excel Spreadsheet also but in this poc i will focus on creating report with MS Word document

Template

For this example, let us create a small report template which can be emailed to a programmer who has won  a programming contest. Just open up MS Word blank document, create a template something like this. Use your creativity to decide on the colors, font, layout etc :)



Pushing the real data!
  1. Open Visual Studio , create a new console application (You can use templater in Web/Desktop Apps. It is just an assembly. Use however you want. For sake of simplicity i've used a console app) 
  2. If you already have Nuget, you are 50% done !. Open the Nuget console and type this
    Install-Package Templater
  3. Incase you dont have nuget, download the assembly from the below link
    http://templater.info/downloads and add 'NGS.Templater.dll'  as a reference to your project.
  4. Open the program.cs and replace it with below code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGS.Templater;
using System.Diagnostics;

namespace Templater
{
    class Program
    {
        static void Main(string[] args)
        {
            //Here we go!
            GenerateWordReport("E:\\Award.docx");
        }


        /// 
        /// This function pushes the data into the report
        /// 

        private static void GenerateWordReport(string i_sReportLocation)
        {

            //Mention the tags and also the data you want to replace it with. FROM_NAME, ADDRESS, TO_NAME, EVENT_DATE, SIGNATURE are the
            //tags we have mentioned in report with square bracets [[ ]] 
            var data = new
            {
                FROM_NAME = "Scott Wilson",
                ADDRESS = "#112, Whitefield, Bangalore",
                TO_NAME = "Robert Woodworth",
                EVENT_DATE = "11/11/2011",
                SIGNATURE = "Scott"
            };

            using (var document = Configuration.Factory.Open(i_sReportLocation))
            {
                //Write the data to the document
                document.Process(data);
            }


            //Open the report (file) for the user to view it
            //This does not have anything to do with templater!
            Process.Start(i_sReportLocation);
        }
    }
}

And you are done! Just make sure the template is available in the path you specify. Run the report.

Here is the output!



Templater is not just a find and replace kind of library. You can actually generate reports using functionality like looping to add many entries to your report. Examples include generating employee list or exporting the list of sales records to a document. The advantage with these kinds of report is that, the formatting capabilities of MS Word can be used to create the templates and MS Word is one of the best editor for windows as you know when compared to any other report designers!. So, go ahead and use it :)

Popular posts from this blog

Facebook Javascript API : Feed and Share Dialog for Beginners

Real time Push Notifications with SignalR & PNotify (Pines Notify)

What's new and expected in .NET Framework 4.5