AirWolf
AirWolf

Reputation: 597

Export Report Data To Excel

I have two separate projects MyApp.Reports and MyApp.WEB

the MyApp.Reports containts only *.rdlc reports and MyApp.WEB is the ASP.NET MVC 4 Razor. I have a requirement to export the data from my report to Excel and let the user Download it, how can i load the data from my model to the Report and export it to an Excel file?

What i allready tried:

Prototype MVC4 Razor ReportViewer? RDLC <-- Generates an image instead of PDF or Excel

Model:

public class MyModel
{
  public int OrderNr { get; set;}
  public string Title { get; set;}
}

Action:

    public ActionResult ExportToExcel()
    {
      IEnumerable<MyModel> model = database.GetMyModelData(); //<-- returns list of data 
      ...
    }

Upvotes: 1

Views: 5050

Answers (1)

AirWolf
AirWolf

Reputation: 597

After tinkering with the CodeProject demo that "Bradly Uffner" gave me i managed to get the code working the way i wanted it to, you can download the demo project Here. So here is what i came up with:

View:

@Html.ActionLink("Download Report in Excel Format", "ExportReport", new { ContentType = "application/vnd.ms-excel", FileType = "Excel" })

@Html.ActionLink("Download Report in PDF Format", "ExportReport", new { ContentType = "application/pdf", FileType = "pdf" })

Controller:

public ActionResult ExportReport(string FileType, string ContentType)
{
    LocalReport localReport = new LocalReport();
    localReport.ReportPath = Server.MapPath("~/Content/Report1.rdlc");
    IList<WorldModel> customerList = new List<WorldModel>();

    // SOME DEMO DATA!
    customerList.Add(new WorldModel("Europe", "Sweden", "2001", "1823"));
    customerList.Add(new WorldModel("Europe", "Sweden", "2002", "1234"));
    customerList.Add(new WorldModel("Europe", "Sweden", "2003", "9087"));

    customerList.Add(new WorldModel("Europe", "Denmark", "2001", "6793"));
    customerList.Add(new WorldModel("Europe", "Denmark", "2002", "4563"));
    customerList.Add(new WorldModel("Europe", "Denmark", "2003", "1897"));

    customerList.Add(new WorldModel("Europe", "Norway", "2001", "5632"));
    customerList.Add(new WorldModel("Europe", "Norway", "2002", "9870"));
    customerList.Add(new WorldModel("Europe", "Norway", "2003", "2367"));

    customerList.Add(new WorldModel("Asia", "India", "2001", "1980"));
    customerList.Add(new WorldModel("Asia", "India", "2002", "9765"));
    customerList.Add(new WorldModel("Asia", "India", "2003", "6789"));
    //DEMO DATA END

    ReportDataSource reportDataSource = new ReportDataSource();
    reportDataSource.Name = "DataSet1";

    //********** IF YOU NEED TO FILTER THE DATA ******************
    //var customerfilterList = from c in customerList
    //                         where c.Territory == territory
    //                         select c;
    //reportDataSource.Value = customerfilterList;
    //************************************************************

    reportDataSource.Value = customerList;

    localReport.DataSources.Add(reportDataSource);
    string reportType = FileType;
    string mimeType;
    string encoding;
    string fileNameExtension;
    //The DeviceInfo settings should be changed based on the reportType            
    //http://msdn2.microsoft.com/en-us/library/ms155397.aspx            
    string deviceInfo = "<DeviceInfo>" +
        "  <OutputFormat>" + FileType + "</OutputFormat>" +
        "  <PageWidth>8.5in</PageWidth>" +
        "  <PageHeight>11in</PageHeight>" +
        "  <MarginTop>0.5in</MarginTop>" +
        "  <MarginLeft>1in</MarginLeft>" +
        "  <MarginRight>1in</MarginRight>" +
        "  <MarginBottom>0.5in</MarginBottom>" +
        "</DeviceInfo>";
    Warning[] warnings;
    string[] streams;
    byte[] renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);          
    return File(renderedBytes, ContentType, string.Format("NameOfFile.{0}",fileNameExtension));

}

I'm not sure if this will work if the reports are in a seperate project but i will try that also.

Resources: Content Types Here

Upvotes: 4

Related Questions