c# - 将报表数据导出到 Excel

标签 c# asp.net asp.net-mvc asp.net-mvc-4 export-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" })

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

}

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

资源: 内容类型 Here

关于c# - 将报表数据导出到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29609607/

相关文章:

c# - 如何在启动时将表单加载更改为其他表单?

c# - 如何切换中继器中控件的可见性?

c# - 为什么 Timespan.TryParseExact 没有按预期解析输入?

c# - 高效滚动最大和最小窗口

c# - 使用 PostSharp 将属性应用于界面

c# - C# 4 的协变是否支持泛型的嵌套?

jquery - 在 asp.net MVC3 EF4.1 中创建父级时创建动态子级列表

javascript - 日期在 IE 浏览器中显示无效,但适用于 chrome 和 Firefox

c# - 将字符串从 jQuery 传递到 C# MVC

asp.net - 如何查找 IIS 功能的名称