Quantcast
Channel: amitpatelit » Crystal Reports
Viewing all articles
Browse latest Browse all 3

Crystal Reports in MVC

$
0
0

Below are the code for implement crystal reports in MVC with support of export to PDF and Excel format.

MVC rajor/aspx pages is not supporting report viewer so that is not possible to place report viewer where user can see reports and export that in required format.
So in MVC we have directly deal with report class and generate PDF/Excel and sent to client with browsers functionality view and download.

View: In view required all search parameter and with Generate report button (PDF/Excel)

Contollor : based on provided search parameter fetch data and bind with crystal reports class and generate PDF/excel and sent to client’s browser for download.

Below the are the code for controller to generate and send file to client’s browser.

ReportsServices dbReportsServices = new ReportsServices();
ReportClass reortptH = new ReportClass();
            reortptH.FileName = Server.MapPath("~/Reports/ReportData.rpt");

reortptH.Load();

DataSet Ds = dbReportsServices.FetchData("GetData"); // exectures store procedure to retrieve data

reortptH.SetDataSource(Ds.Tables[0]);

bool IsPDF = Convert.ToBoolean(collection.Get("IsPDF")); // In view add two button for export to PDF and excel and that will returs value on that way.

            if (IsPDF)
            {
                System.IO.Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                return File(stream, "application/pdf", "ReportData.pdf");
            }
            else
            {
                System.IO.Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel);
                return File(stream, "application/xls", "ReportData.xls");
            }

Post your comments if you have questions/comments.

Thanks,
Amit Patel
amitpatel.it@gmail.com
“Enjoy Programming”



Viewing all articles
Browse latest Browse all 3

Trending Articles