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

Dynamic HTML reports in Windows Forms

, 3 Nov 2004
Rate this:
Please Sign up or sign in to vote.
Reports any class information in a HTML page without using Crystal

Introduction

Some times Crystal reports is not easy to use and it also does not delivers output just the way we want.

To work around this problem we just need an HTML template file and a parser class that reads this file and calls the appropriate methods in our classes to deliver dynamic data.

Using the code

The application posted in this article contains a Windows Forms project written in C#. Includes a kind of dummy class Persons which groups attributes like Name, Last Name, Birth, gender, email and country. The data contained in the objects of this class is the one we want to print in an HTML report. It is only an structure but I think it works to explain the solution.

The last class HTMLReport parses the HTML Template file in its Generate method and calls the Report(string data) of the object passed as an argument every time it finds a string with the form "<@Data_To_Report>". This string must be included as a case in switch instruction of the Report Method.

For example if the parser finds in the HTML template the code: <h2><@FullName></h2>

The parser will get <@FullName> and send it as a parameter to the method Report() of the class Person which has some code like this

    switch (data)
         {
            case"<@FullName>":
               return mName + " " + mLast;
            ....
            ....
            ....
        }

Finally the parser will make a new HTML file with the html design it found in the Template and the string the class Person returns. Just like this: <h2><Somebody's name Somebody's last name</h2>

The idea of Form1.cs is only to illustrate a GUI that fills data in the Person class. The event click of the button has this code.

        //Create the object person and assign the values
         Persons newPerson = new Persons();
         newPerson.Name = txtName.Text;
         newPerson.Last = txtLast.Text;
         newPerson.Country = comboCountry.SelectedItem.ToString();
         newPerson.IsMale = radMale.Checked;
         newPerson.Email = txtEmail.Text;
         newPerson.BirthDate = Convert.ToDateTime( txtBirthDate.Text);

         //Create the object Report and assign object to report
         HTMLReport html = new HTMLReport(); 
         //Assign the template file 
         html.Template = "GeneralTemplate.htm";
         //Asign the new file name
         html.FileName = newPerson.Name; 
         //Generate the new Page and open it. It uses default browser
         System.Diagnostics.Process.Start(html.Generate(newPerson));

One of the biggest improvements of this approach is that whenever you want to change the format or the order data is reported it is as easy as to edit an HTML page. We know there are a lot of HTML editors we can use but I still prefer notepad.exe but that is personal issue.

More than a solution to a specific problem I see this as a framework, it means you should read this article as a way to work whenever you need to deliver data from your objects in a nice, cheap and clean way. Clearly you must need to rewrite the code to your specific needs but you could follow the same pattern for printing data from your objects.

History

This is just version 1.0. Next time I will try to illustrate an example of objects with complex data types like typed Datasets.

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

Share

About the Author

pablojag
Web Developer
Mexico Mexico
Pablo studied Information System Engineering and graduated on may 2001 although he always had the idea of becoming a writer, idea that has not escaped from sight. At the present he works as a software developer leader in a Insurance Company in the team of Systems for Sale Force developing tools for policy selling. He has experience with Dot Net since May 2002 and has reached the third Start recognition from MSDN Latin America. He has knowledge in Compact Framework, Windows Forms and Asp Net. Pablo looks forward that his contributions in this forums help the comunity at the Code Project.

Comments and Discussions

 
Questionsimple but useful for me PinmemberĐặng Phi Sơn7-Aug-12 4:48 
QuestionDo this approach work well with ASP 2.0 ? PinmemberMCA_GUY8-Jan-05 5:21 
AnswerRe: Do this approach work well with ASP 2.0 ? Pinmemberpablojag10-Jan-05 2:58 
GeneralRe: Do this approach work well with ASP 2.0 ? Pinsussvishali15-Sep-05 18:52 
GeneralRe: Do this approach work well with ASP 2.0 ? Pinmemberpablojag19-Sep-05 3:46 
QuestionHow do i display multiple repeating records and grouping of that record in HTML Reports. Help!!! PinmemberNupur Singh3-Mar-09 20:52 
AnswerRe: How do i display multiple repeating records and grouping of that record in HTML Reports. Help!!! Pinmemberganmgjai23-Dec-10 1:35 

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 | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 4 Nov 2004
Article Copyright 2004 by pablojag
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid