Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Report Builder

2.60/5 (11 votes)
26 Feb 2008CPOL1 min read 1   5.8K  
Tool Report Builder

Introduction

This article presents a tool for generation of reports that was developed in the last five months. The tool has been created in C# and works in .NET Framework 2.0. For a long time, I used the ReportBuilder with Delphi. Then, I migrated to Visual Studio and C# as I felt a certain difficulty working with the tools for generation of reports. At that time, I decided to build my own tool. The result of my work is shown in this article. The creator supports several types of data - DataSet, ArrayList, and DataTable. I could still add new types, but this seems to be enough to implement the IDataReport interface.

Using the Code

  1. Drawing of the layout of the Document Principal modules, which is also the module that absorbed the most development time, was the module for drawing the report layout. In the Designer's module, it is possible to create title pages, titles, to shape the layout and size of the page, as well as do groupings. All this can be observed in the following three figures.

Gerador_v02_small.jpg

Figure 1 - Title, Header, Summary

Gerador_v03_small.jpg

Figure 2 - Visualization of the Report. First page

Gerador2_small.jpg

Figure 3 - Visualization of the Report. Next page
  1. Code to load the Module of Drawing of the report is as follows:

    C#
    using KReport;
    
    public static void Main () {
          DataSet dsDados = new DataSet ();
          ds.ReadXML (' Teste.xml ');
    
          KReport.Engine.Report rpt = new KReport.Engine.Report();
              
          rpt.FileName = 'C:\Report.xml '; // defines the archive where the layout is
          rpt.Load (); // Loads the layout
          rpt.AddSource (dsdados); // adds the fountain of data
          rpt.ShowDesigner (); // Visualizes layout of the report
    }
  2. Code to load the form of visualization of the report is as follows:

    C#
    using KReport;
    
    public static void Main () {
          DataSet dsDados = new DataSet ();
          ds.ReadXML (" Teste.xml ");
    
          KReport.Engine.Report rpt = new KReport.Engine.Report();
              
          rpt.FileName = " Report.xml "; // defines the archive where the layout is
          rpt.Load (); // Loads the layout
          rpt.AddSource (dsdados); // adds the fountain of data
          rpt.Show (); // Visualizes report in the form of preview
    }
  3. Code to load and print the report is as follows:

    C#
    public static void Main () {
          DataSet dsDados = new DataSet ();
          ds.ReadXML (" Teste.xml ");
    
          KReport.Engine.Report rpt = new KReport.Engine.Report();
              
          rpt.FileName = " Report.xml "; // defines the archive where the layout is
          rpt.Load (); // Loads the layout
          rpt.AddSource (dsdados); // adds the fountain of data
          rpt.Print (); // Printed report 
    }
  4. Code using a list of objects is as follows:

    C#
    public static void Main () {
          ArrayList source;
          Persistencia.StartSession ();
          source = Persistencia.GetListObject (typeof (Credito), " status = 0 ");
          Persistencia.EndSession ();
    
          KReport.Engine.Report rpt = new KReport.Engine.Report();
              
          rpt.FileName = " Report.xml "; // defines the archive where the layout is
          rpt.Load (); // Loads the layout
          rpt.AddSource (source); // adds the fountain of data
          rpt.Print (); // Printed report 
    }

History

  • 22nd February, 2008 -- Original version posted
  • 26th February, 2008 -- Download added

License

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