将报告数据导出到Excel

空狼

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

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

我已经尝试过的是:

原型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演示之后,我设法使代码按我想要的方式工作,您可以在此处下载演示项目所以这是我想出的:

看法:

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

}

我不确定如果报告在单独的项目中是否可以使用,但我也会尝试。

资源:此处的内容类型

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将数据导出到Excel

来自分类Dev

icCube-使用格式值将报告导出到Excel

来自分类Dev

将SQL Server数据导出到Excel

来自分类Dev

将绘图数据从R导出到Excel

来自分类Dev

将数据表导出到Excel

来自分类Dev

将数据DataGridView导出到Excel

来自分类Dev

将数据导出到Excel工作表

来自分类Dev

将数据从jira导出到Excel

来自分类Dev

将 Excel 数据导出到嵌套的 JSON

来自分类Dev

将 Python 数据框导出到 Excel

来自分类Dev

将数据从数据表导出到Excel

来自分类Dev

使用Python将特定的MySQL数据从表导出到Excel

来自分类Dev

如何使用angularjs将数据从HTML表导出到Excel

来自分类Dev

是否可以将数据从JIRA动态导出到Excel?

来自分类Dev

如何将大量数据导出到Excel

来自分类Dev

如何启用jQgrid将数据导出到PDF / Excel

来自分类Dev

将JavaFX TableView数据导出到Excel(XLS)

来自分类Dev

使用javascript将JQGrid数据导出到Excel

来自分类Dev

将html表单数据导出到excel

来自分类Dev

将数据导出到多个Excel工作表

来自分类Dev

将数据导出到CSV-Excel VBA

来自分类Dev

无法将熊猫数据框导出到Excel /编码

来自分类Dev

将数据从R导出到Excel:公式不会重新计算

来自分类Dev

将数据从VB导出到Excel工作表

来自分类Dev

如何将Excel数据导出到Mysql表

来自分类Dev

将数据集导出到Excel 2007 EPPlus

来自分类Dev

将表数据导出到流星中的excel中

来自分类Dev

phpmyadmin将选定的数据从SQL导出到excel

来自分类Dev

将数据导出到Excel时出现Pandas错误