Crystal Reports: Dynamically Bind Reports and Export as PDF or any Format

HTML clipboard

Referrences:

Add the below references from .Net tab (Projects -> Add Reference..)

CrystalDecisions.CrystalReports.Engine
CrystalDecisions.Shared

Namespaces:

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data.SqlClient;

Global Variable declaration:

ReportDocument rpt = new ReportDocument();

Display the report through CrystalViewer in Form_Load() event:

private void Form1_Load(object sender, EventArgs e)
        {
            rpt.Load(@"D:\Muthu\Tech\Blogs\CrystalReportsApp\CrystalReportsApp\ChartReport.rpt");
            crystalReportViewer1.ReportSource = rpt;
            crystalReportViewer1.Refresh();
        }

Bind the Crystal report dynamically and Update the crystal report label value dynamically:

private void btnRefresh_Click(object sender, EventArgs e)
        {
            //Begin: Bind the reports dynamically
            DataTable dtReports = BindReports();
            rpt.SetDataSource(dtReports);
            //End: Bind the reports dynamically

            //Begin: Update the report label value from All to the entered employee name in the text box
            TextObject myTextObjectOnReport;
            if (rpt.ReportDefinition.ReportObjects["txtEmpName"] != null)
            {
                myTextObjectOnReport = (TextObject)rpt.ReportDefinition.ReportObjects["txtEmpName"];
                myTextObjectOnReport.Text = txtName.Text;
            }

            crystalReportViewer1.ReportSource = rpt;
            crystalReportViewer1.Refresh();
            //End
        }
//Filtered the report details based on the employee name entered in the textbox
        private DataTable BindReports()
        {
            string conString = @"Data Source=WVDI1IHCL32\WVDI1IHCL32;Initial Catalog=Test;Integrated Security=True;";
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            string query = "SELECT EmployeeID, DepID, Name, Mark FROM Employee E WHERE E.Name='" + txtName.Text + "'";
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(query, con);
 
            DataTable dtReport = new DataTable();
            sqlAdapter.Fill(dtReport);
            con.Close();
 
            return dtReport;
        }

Generate the Crystal report as a PDF, Word or Excel:

When we use the CrystalReportViewer to display reports, we have a provision to export the report as PDF or any other format using the below icon.

CR1.gif

If we want to generate the report without the viewer control, that time we can use the below code. (Automation of report generation using windows service or console app)

private void btnExport_Click(object sender, EventArgs e)
        {
            ExportOptions rptExportOption;
            DiskFileDestinationOptions rptFileDestOption = new DiskFileDestinationOptions();
            PdfRtfWordFormatOptions rptFormatOption = new PdfRtfWordFormatOptions();
 
            //If we want to generate the report as pdf, change the file extention type as "D:\Muthu\SampleReport.pdf"
 
            //If we want to generate the report as excel, change the file extention type as "D:\Muthu\SampleReport.xls"
            string reportFileName = @"D:\Muthu\SampleReport.doc";

            rptFileDestOption.DiskFileName = reportFileName;
            rptExportOption = rpt.ExportOptions;
            {
                rptExportOption.ExportDestinationType = ExportDestinationType.DiskFile;
                //if we want to generate the report as PDF, change the ExportFormatType as "ExportFormatType.PortableDocFormat"
                //if we want to generate the report as Excel, change the ExportFormatType as "ExportFormatType.Excel"
                rptExportOption.ExportFormatType = ExportFormatType.RichText;
                rptExportOption.ExportDestinationOptions = rptFileDestOption;
                rptExportOption.ExportFormatOptions = rptFormatOption;
            }
            rpt.Export();
        }


Report Design:

CR2.gif

Output: (First time, report will looks like the below)

CR3.gif

Enter employee name in the textbox and click "Refresh" button.

CR4.gif

To generate the report as doc, click on "Export" button.


 


Similar Articles