Creating a cool Microsoft Word Report for listing Scientists with Templater and C# Generic List

Some time back, I had written a post on Templater Library for creating a simple "Prize Winner Notification" e-mailer template using MS Word & Templater. It was a simple one. If you have not read it yet, please have a look at this Post.

In this post, let us create a Microsoft Word Report which lists few great scientists across the world. Also, let us see how the Templater supports Generic List as a data source for the report.


Creating the report template using MS Word
  1. Open Microsoft Word, create a new blank document.
  2. Insert a table with just 1 Row and 2 Columns
  3. Insert the Tags inside the cells [[NAME]] & [[BIRTH_DEATH]]
  4. Select the table design or do the color, font formatting and style the way you want. (This is not mandatory!). You can even select a bullet!
  5. Save the document as Scientists.docx (or .doc)
Writing some code

  1. Open Visual Studio and Create a new console application for this demonstration! Reference the Templater DLL to Your Project or do Install-Package Templater from Nuget!
  2.  Now, let us create a Entity which can hold the Name and Active years of a scientist.Add a class file, Create a Class like this.
class Scientist

    {
        private string m_sName;
        private string m_sYears;

        public string NAME
        {
            get { return m_sName; }
            set { m_sName = value; }
        }

        public string BIRTH_DEATH
        {
            get { return m_sYears; }
            set { m_sYears = value; }
        }
    }

The property names are in UPPERCASE just because the tags we gave in the word document are also in Uppercase. Also, the property names here exactly match the tags we gave in the word document template.
In the main program.cs, let us add a function which generates a list of employess
/// <summary>
        /// This method Creates a List of Scientists and adds the objects into a Generic List Collection 
        /// and Returns it back
        /// </summary>
        /// <returns></returns>
        private static List<Scientist> GetScientistData()
        {
            List<Scientist> lstScientists = new List<Scientist>();

            Scientist scntst0 = new Scientist { NAME = "Albert Einstein", BIRTH_DEATH = "1879 - 1955" };
            lstScientists.Add(scntst0);

            Scientist scntst1 = new Scientist { NAME = "Sir Isaac Newton", BIRTH_DEATH = "1642 - 1727" };
            lstScientists.Add(scntst1);

            Scientist scntst2 = new Scientist { NAME = "Galileo Galilei", BIRTH_DEATH = "1564 - 1642" };
            lstScientists.Add(scntst2);

            Scientist scntst3 = new Scientist { NAME = "Charles Darwin", BIRTH_DEATH = "1809 - 1882" };
            lstScientists.Add(scntst3);

            Scientist scntst4 = new Scientist { NAME = "Johannes Kepler", BIRTH_DEATH = "1571 - 1630" };
            lstScientists.Add(scntst4);

            Scientist scntst5 = new Scientist { NAME = "Louis Pasteur", BIRTH_DEATH = "1822 - 1895" };
            lstScientists.Add(scntst5);

            Scientist scntst6 = new Scientist { NAME = "James Maxwell", BIRTH_DEATH = "1831 - 1879" };
            lstScientists.Add(scntst6);

            Scientist scntst7 = new Scientist { NAME = "Edwin Hubble", BIRTH_DEATH = "1889 - 1953" };
            lstScientists.Add(scntst7);

            Scientist scntst8 = new Scientist { NAME = "Emil Fisher", BIRTH_DEATH = "1852 - 1919" };
            lstScientists.Add(scntst8);

            Scientist scntst9 = new Scientist { NAME = "Paul Dirac", BIRTH_DEATH = "1902 - 1984" };
            lstScientists.Add(scntst9);

            return lstScientists;
        }
 
Now, we have the data and also the report template. Let us write the code to generate report
        /// <summary>
        /// This method Generates the Report using Templater
        /// </summary>
        /// <param name="lstScientists">List of Scientist (Generic List) </param>
        /// <param name="i_sReportLocation">Location of the Word Template</param>
        private static void GenerateReport(List<Scientist> lstScientists, string i_sReportLocation)
        {
            using (var document = Configuration.Factory.Open(i_sReportLocation))
            document.Process(lstScientists);

            //Open the report (file) for the user to view it
            Process.Start(i_sReportLocation);
        }
Thats all we have to do. But hold on. We have to invoke these methods. Lets do it

In the main function , call the above methods to create the report
using System.Collections.Generic;
using NGS.Templater;
using System.Diagnostics;

static void Main(string[] args)
        {
//Generates Data
            List<Scientist> lstScientists = GetScientistData(); 

//Generates Report based on the data. When trying out this code, Give appropriate path of your report template (Which you created earlier) below.
            GenerateReport(lstScientists,@"C:\Scientists.docx");
        }
Hurry up!

Run it and believe it!

Here is how it looks!


Working with MS Office reports was never easier for C# developers earlier. Isn't 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