簡體   English   中英

在Asp.Net Mvc4的網頁中顯示Rdlc報告

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

我是Asp.Net MVC4的新手。 我有一個rdlc報告,我需要在索引頁面中添加該報告。 但是我在Google上搜索了很多,他們建議使用PDF文件或圖像文件。 我需要在頁面本身上顯示報告。 那怎么辦

謝謝,

蔡健雅

這是1歲的帖子,但是很多人可能仍會點擊此頁面尋求解決方案,所以我想回答這個問題。

->您是否考慮過使用ReportViewer在網頁上顯示rdlc報告?

1)創建Aspx頁面。 從“ AjaxExtension”工具選項中將“ ScriptManager”添加到此頁面。 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();

您可以使用報告創建一個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();
}

您可能需要將reportType更改為所需的值,並記住要相應地更改deviceInfo。 您可以在此處找到信息。

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM