The export option is very useful when business users want to have reports exported different formats such as PDF, Excel, Word and HTML. They want this facility in order to download the report and store it for future use or to modify the report further (possible in case of Word and Excel). In many cases the export option is used for scheduled reports to be send via email, In this case, the exporting is done on the server side and the exported file is attached to an email and send to the user's email id.
In this tutorial, I will be showing you how to export a report to PDF format, but it can be easily modified to export the report in other formats.
Create a Crystal Report and display to the user
Create a New ASP.NET Web Site Project

Add XML file to the Project
Right-Click on App_Data folder and select Add Existing Item

Add a crystal report to the project
Right-Click on the website root and select Add New Item.Select "Crystal Report".
Enter the name of the report, I used the default name given by the IDE.

Expand the Create New Connection node and then expand the ADO.net option.

Display the report on a web page
In the design view drag and drop the crystal reports viewer from Toolbox onto the web page.Add a button which will handle the displaying of the report.
The HTML for body of the webpage should like as follows:
<body>
<formid="form2"runat="server">
<div>
<asp:ButtonID="btnShow"runat="server"OnClick="btnShow_Click"Text="Show Report in Viewer" /><br/>
<br/>
<CR:CrystalReportViewerID="CrystalReportViewer1"runat="server"AutoDataBind="true"/>
</div>
</form>
</body>
Remember to add following namespaces at the top of code behind page for referencing Crystal Report objects.
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
The event handler should looks as follows:
protectedvoid btnShow_Click(object sender, EventArgs e)
{
// Get the report document and bind it with the crystal report viewer
CrystalReportViewer1.ReportSource = getReportDocument();
CrystalReportViewer1.DataBind();
}
// Returns a ReportDocument object with the report and data loaded
private ReportDocument getReportDocument()
{
// File Path for Crystal Report
string repFilePath = Server.MapPath("CrystalReport.rpt")
// Declare a new Crystal Report Document object
// and the report file into the report document
ReportDocument repDoc = new ReportDocument();
repDoc.Load(repFilePath);
// Set the datasource by getting the dataset from business
// layer and
// In our case business layer is getCustomerData function
repDoc.SetDataSource(getCustomerData());
}
// Business layer class to get the data from database
private DataSet getCustomerData()
{
string customerFilePath = Server.MapPath("App_Data\\Customers.xml");
DataSet ds =new DataSet();
ds.ReadXml(customerFilePath);
}
Export the Crystal Report in PDF format
Add a button to the web page so that user can click and get the PDF.<body>
<formid="form2"runat="server">
<div>
<asp:ButtonID="btnShow"runat="server"OnClick="btnShow_Click"Text="Show Report in Viewer" /><br/>
<br/>
<asp:ButtonID="btnExport"runat="server"OnClick="btnExport_Click"Text="Export To PDF"/>
<br/>
<br/>
<CR:CrystalReportViewerID="CrystalReportViewer1"runat="server"AutoDataBind="true"/>
</div>
</form>
</body>
The Code behind for export is as follows:
protectedvoid btnExport_Click(object sender, EventArgs e)
{
// Get the report document
ReportDocument repDoc = getReportDocument();
// Stop buffering the response
Response.Buffer =false;
// Clear the response content and headers
Response.ClearContent();
Response.ClearHeaders();
try
{
// Export the Report to Response stream in PDF format and file name Customers
repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat,Response,true,"Customers");
// There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
ex =null;
}
}
Export to other formats
The report can be exported in different formats as follow:ExportFormatType.PortableDocFormat | |
Complete Report to Excel | ExportFormatType.Excel |
Only Report Data to Excel | ExportFormatType.ExcelRecord |
HTML | ExportFormatType.HTML40 |
RTF | ExportFormatType.RichText |
Word | ExportFormatType.WordForWindows |
1) Create simple crystal reports
2) Display it to a web page using a crystal reports viewer
3) Allow users to download the reports in various formats such as PDF, Excel, Word and HTML.
You can download Sample Export Crystal Report To PDF Visual Studio Project, used in this tutorial.
Many people found that Crystal Reports are complicate to learn. Personally, I like Stimulsoft Reports for ASP.NET application. If you use SQL Server, nice solution is ApexSQL Report.
This tutorial is written by Chirag Nirmal - CANSoft.
No comments:
Post a Comment