In this weblog, I've regularly posted examples of stuff I created to help me use Crystal Reports in .Net applications. To make life easier I created a small class library which contains the code from all the posts I've done sofar, as well as some new stuff. The class library can be used to handle Crystal Reports in a simple way. In the past weeks I've been adding functionality to support the version that's included in Visual Studio 2005. A full description of the functionality can be found in my CodeProject article about the CrystalHelper class.
Functionality offered by this class library
The class library handles the following general Crystal functionality:
- Assign a database connection to the report.
- Assign DataSet's to the report.
- Print a report on the default printer.
- Write the report to disk in one of the following formats:
- Microsoft Word
- Microsoft Excel
- Rich Text
- Adobe Portable Document Format (PDF)
- Set values for report parameters
The class library also allows you to manipulate the Crystal Report Viewer control (WinForm only):
- Show / Hide the status bar.
- Show / Hide the tabs.
- Change the report name shown on the tabs.
A few examples will show you how easy it is to use Crystal Reports in .Net using this class library:
Sending a report to the default printer
using (CrystalHelper helper = new CrystalHelper(new CrystalReport1(),
"Server", "Database", "UserName", "Password"))
{ helper.Open();
helper.Print();
helper.Close();
}
Writing a report to disk as an Adobe PDF file
using (CrystalHelper helper = new CrystalHelper(new CrystalReport1(),
"Server", "Database", "UserName", "Password"))
{ helper.Open();
helper.Export(@"C:\Sample.pdf", CrystalExportFormat.PortableDocFormat);
helper.Close();
}
The code is documented using XML tags. It is possible to generate documentation using NDoc or any other tool that can process the XML file created when you compile this code. Attached to this post is a zip file. It contains the sources to my CrystalHelper class and for a demo application that shows how you might use this helper.