在Asp.Net Mvc4的网页中显示Rdlc报告

时间:2014-02-18 12:34:52

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 c#-4.0 razor

我是Asp.Net MVC4的新手。我有一个rdlc报告,我需要在我的索引页面中添加该报告。但我搜索了很多,他们建议像PDF文件或图像文件。我需要在页面上显示报告。怎么办?

谢谢,

丹娘

2 个答案:

答案 0 :(得分:1)

这是一个1岁的帖子,但很多人可能仍然会点击此页面寻求解决方案,所以我想回答这个问题。

- >您是否考虑使用ReportViewer在您的网页上显示rdlc报告?

1)创建Aspx页面。添加" ScriptManager"从" AjaxExtension"工具选项到此页面。 2)添加" ReportViewer"从"报告"到此页面工具选项。 3)并考虑以下代码在此aspx页面的代码隐藏中分配数据源。

string ID = Request.QueryString["ID"];                
List<Obj1> List1 = new List<Obj1>();
List<Obj2> List2 = new List<Obj2>();
List<Obj3> List3 = new List<Obj3>();

    using (var db = new 'use ur edmx connectionstring name')
    {                                  
        List1 = db.'urTableName1'.Where(x => x.ID == ID).ToList();
        List2 = db.'urTableName2'.Where(y => y.ID == ID).ToList();
        List3 = db.'urTableName3'.Where(z => z.ID == ID).ToList();                    
    }

    rptVWSmartBOM.LocalReport.DataSources.Clear();

   ReportDataSource rd1 = new ReportDataSource("Your DataTable name used in DataSet", List1);
   ReportDataSource rd2 = new ReportDataSource("Your DataTable name used in DataSet", List1);
   ReportDataSource rd3 = new ReportDataSource("Your DataTable name used in DataSet", List1);
   ReportViewer1.LocalReport.DataSources.Add(rd1);
   ReportViewer1.LocalReport.DataSources.Add(rd2);
   ReportViewer1.LocalReport.DataSources.Add(rd3);                                
   ReportViewer1.LocalReport.ReportPath = "xyz.rdlc";

   ReportViewer1.LocalReport.Refresh();

答案 1 :(得分:0)

您可以使用报告创建一个aspx页面,然后将报告嵌入到MVC视图的<iframe />中,或者您可以尝试使用此方法直接在响应中返回蒸汽:

private void RenderReport() {

    LocalReport localReport = new LocalReport();

    localReport.ReportPath = Server.MapPath("~/YourReportName.rdlc");

    // Add your data source
    ReportDataSource reportDataSource = new ReportDataSource("YourCollection", yourCollection);

    localReport.DataSources.Add(reportDataSource);

    string reportType = "PDF";
    string mimeType;
    string encoding;
    string fileNameExtension;

    //The DeviceInfo settings should be changed based on the reportType
    string deviceInfo =
        "<DeviceInfo>" +
        "  <OutputFormat>PDF</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;

    //Render
    renderedBytes = localReport.Render(
        reportType, 
        deviceInfo, 
        out mimeType, 
        out encoding, 
        out fileNameExtension, 
        out streams, 
        out warnings);

    //Write to the outputstream
    //Set content-disposition to "attachment" so that user is prompted to take an action
    //on the file (open or save)

    Response.Clear();
    Response.ContentType = mimeType;
    Response.AddHeader("content-disposition", "attachment; filename=foo." + fileNameExtension);
    Response.BinaryWrite(renderedBytes);
    Response.End();
}

您可能需要将reportType更改为您需要的内容,并记住相应地更改deviceInfo。您可以找到信息here

希望它对你有所帮助。