Share via


ASP.NET MVC Crystal Reports

Question

Tuesday, April 12, 2011 6:20 PM

I know crystal report viewer doesnt work in Asp.net MVC.

 

This post is in hope if there is way to add Crystal reports to MVC and export directly to pdf?

Also working with Ajax and Jquery at the same time. Please if any one can suggest a better answer that would be great.

 

Thanks.

All replies (9)

Tuesday, April 12, 2011 7:03 PM ✅Answered

Just have a webform page that generates the report and iframe it into your mvc view.


Wednesday, April 13, 2011 12:48 AM ✅Answered

Try this Silverlight report viewer:

http://www.gcpowertools.com/Products/ActiveReports/WhatsNew

it looks to have all the features that you want and much more.


Friday, April 15, 2011 3:23 AM ✅Answered

Hi Rasool,

The ExportToStream mothod of ReportClass can export the report to the bytes stream. So you can use it to export the report to pdf directly.

First add a crystal reporter and choose your custom .NET Ojbects as its data source(Take Models.customer for example here), and then in the a DisplayReport action which return a FileStreamResult using File() mehtod. For example:

public ActionResult DetailsReport()
        {
            ReportClass rptH = new ReportClass();
            rptH.FileName = Server.MapPath("~/Content/Reports/CrystalReport.rpt");
            rptH.Load();

            //GetAllCustomers() return a list of Custom type
            rptH.SetDataSource(ToDataTable<Customer>(Customer.GetAllCustomers()));
            Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            return File(stream,"application/pdf");
        }

        //Cast the list<T> to DataTable type
        private DataTable ToDataTable<T>( List<T> data)
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T)); 
            DataTable table = new DataTable(); 
            for (int i = 0; i < props.Count; i++) 
            { 
                PropertyDescriptor prop = props[i]; 
                table.Columns.Add(prop.Name, prop.PropertyType); 
            } 
            object[] values = new object[props.Count]; 
            foreach (T item in data) 
            {
                for (int i = 0; i < values.Length; i++) 
                { 
                    values[i] = props[i].GetValue(item); 
                } 
                table.Rows.Add(values); 
            } 
            return table;        
        }

Hope this helpful,
Forest Cheng


Wednesday, April 13, 2011 7:29 AM

You may use SQL Server Reporting Services 2005/2008 if it's an intranet project. It will make your life a lot easier.


Wednesday, April 13, 2011 11:32 AM

Could you please let me know how to do it, may be a good link on it.

 

I am using C#,asp.net mvc, crystal reports and dataset. Thanks.


Wednesday, April 13, 2011 2:31 PM

http://www.beansoftware.com/ASP.NET-Tutorials/Export-Crystal-Reports-To-PDF.aspx


Friday, April 15, 2011 11:42 AM

I did make an asp.net project using above code to display in pdf however I need to know how to do it in Asp.net MVC when MVC doesnt support webforms.

 

I have bunch of reports to be displayed in crystal reports (PDF in this case) using MVC.

Thanks in Advance.


Friday, April 15, 2011 1:56 PM

Neither the code Forest provided, nor the code in the article I provided is dependant on WebForms. In particular, the code Forest provided references "ActionResult", so it is specifically targeted to MVC.


Wednesday, May 4, 2011 1:13 PM

Yes, Thanks a lot. I got it working. 

 

The file stream thing works perfect.

I have two more questions.

1) can I use:

Picksumrpt.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, "Picksheet Exported Report");

to export the report instead of :

System.IO.Stream stream = Picksumrpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);            return File(stream, "application/pdf");

 

2) I need to get parameters (Almost 10 report needs different parameters) How to collect without creating 10 views again.

Thanks.