将报告数据导出到Excel

时间:2015-04-13 15:46:54

标签: c# asp.net asp.net-mvc asp.net-mvc-4 export-to-excel

我有两个单独的项目 MyApp.Reports MyApp.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 
      ...
    }

1 个答案:

答案 0 :(得分:3)

在修补CodeProject演示后,&#34; Bradly Uffner&#34; 给了我设法以我想要的方式运行代码,您可以下载演示项目{{ 3}}。所以这就是我提出的:

查看:

@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