简体   繁体   English

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

[英]Display Rdlc report in the web page in Asp.Net Mvc4

I'm very new to the Asp.Net MVC4. 我是Asp.Net MVC4的新手。 I have a rdlc report and i need to add that report in my index page. 我有一个rdlc报告,我需要在索引页面中添加该报告。 But i googled a lot, they have suggested like PDF file or Image File. 但是我在Google上搜索了很多,他们建议使用PDF文件或图像文件。 I need to display the report on the page itself. 我需要在页面本身上显示报告。 How can do that? 那怎么办

Thanks, 谢谢,

Tanya 蔡健雅

This is 1 year old post but many people might still hitting this page for solution, so i thought to reply on this question. 这是1岁的帖子,但是很多人可能仍会点击此页面寻求解决方案,所以我想回答这个问题。

--> did you consider to use the ReportViewer to display the rdlc report on your webPage? ->您是否考虑过使用ReportViewer在网页上显示rdlc报告?

1) Create Aspx page. 1)创建Aspx页面。 Add "ScriptManager" to this page from "AjaxExtension"Tools option. 从“ AjaxExtension”工具选项中将“ ScriptManager”添加到此页面。 2) Add "ReportViewer" to this page from the "Report" Tools Option. 2)从“报告”工具选项中将“ ReportViewer”添加到此页面。 3) And consider following code to assign datasource in the codebehind of this aspx page. 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();

You can create a aspx page with the report and then embed the report in a <iframe /> on your MVC view, or you can try with this method that returns the steam directly on the response: 您可以使用报告创建一个aspx页面,然后将报告嵌入到MVC视图的<iframe /> ,或者您可以尝试使用这种直接在响应中返回Steam的方法:

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

You maybe need to change the reportType to wath you need, and remember to change the deviceInfo accordingly to it. 您可能需要将reportType更改为所需的值,并记住要相应地更改deviceInfo。 You can find the information here . 您可以在此处找到信息。

Hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM