Click here to Skip to main content
15,886,519 members
Articles / Programming Languages / Visual Basic

Printing Reports in .NET

Rate me:
Please Sign up or sign in to vote.
4.85/5 (70 votes)
26 Aug 2008CPOL11 min read 439.3K   15.6K   257  
Using the library presented, you can print reports from C# and other .NET languages
using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using ReportPrinting;

namespace ReportDocumentTesting
{
	/// <summary>
	/// This is a Sample of a report maker.
	/// It uses the ReportBuilder to ease the creation of a complex report.
	/// It also has helper routines to read data out of a XML File.
	/// </summary>
	public class SampleReportMakerXML : IReportMaker
	{
		public SampleReportMakerXML()
		{
		}

        public string Filename;
        public DataSet MyDataSet;
        public ArrayList TablesToPrint = new ArrayList();

        public void readDb ()
        {
            MyDataSet = new DataSet();
            try
            {
				MyDataSet.ReadXml(@Filename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "XML File Error", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {

            }
        } // readDb


        /// <summary>
        /// Sample report maker that uses the ReportBuilder quite heavily.
        /// </summary>
        /// <param name="reportDocument"></param>
        public void MakeDocument(ReportDocument reportDocument)
        {
            TextStyle.ResetStyles();
            // Setup the document's settings

            // Setup global TextStyles
            TextStyle.Heading1.Brush = Brushes.DarkBlue;
            TextStyle.Heading1.SizeDelta = 4.0f;
            TextStyle.Heading1.Underline = true;
            TextStyle.TableHeader.Brush = Brushes.DarkGreen;
            TextStyle.Normal.Size = 9.0f;
            TextStyle.PageFooter.StringAlignment = StringAlignment.Far;
            // add space at the right edge of columns, this will 
            // keep them spaced apart a little nicer
            TextStyle.TableHeader.MarginFar = 0.15f;
            TextStyle.TableRow.MarginFar = 0.15f;
            TextStyle.TableRow.MarginBottom = 0.05f;

            // Now use a builder to setup everything else
            ReportBuilder builder = new ReportBuilder(reportDocument);
            builder.MaxHeaderRowHeight = 0.5f;
            builder.MaxDetailRowHeight = 1.0f;
            builder.DefaultTablePen = reportDocument.ThinPen;
            
            builder.StartLinearLayout(Direction.Vertical);

            builder.AddTextSection("Data from selected tables");

            foreach (string str in TablesToPrint)
            {
                DataTable dt = MyDataSet.Tables[str];
                builder.AddTextSection(dt.TableName, TextStyle.Heading1);
                // Add table & all columns
                builder.AddDataSection(dt.DefaultView, true, 10.0f, true, true);
                builder.AddPageBreak();
            }

            builder.AddTextSection("That's all folks", TextStyle.Heading1);
            builder.AddTextSection("Please stay tuned for other fine developments...");

            builder.FinishLinearLayout();

        }

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions