将报表数据导出到 Excel

Export Report Data To Excel

我有两个独立的项目 MyApp.ReportsMyApp.WEB

MyApp.Reports 仅包含 *.rdlc 报告,MyApp.WEB 是 ASP.NET MVC 4 Razor。我需要将数据从我的报告导出到 Excel 并让用户下载它,我如何将数据从我的模型加载到报告并将其导出到 Excel 文件?

我已经尝试过的:

Prototype MVC4 Razor ReportViewer? RDLC <-- 生成图像而不是 PDF 或 Excel

型号:

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

操作:

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

在修改 "Bradly Uffner" 给我的 CodeProject 演示后,我设法让代码按照我想要的方式工作,您可以下载演示项目 Here。所以这就是我想出的:

查看:

@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" })

控制器:

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));

}

如果报告在一个单独的项目中,我不确定这是否有效,但我也会尝试。

资源: 内容类型 Here