2014-02-18 36 views
0

我對Asp.Net MVC4非常陌生。我有一個rdlc報告,我需要在我的索引頁面中添加該報告。但我GOOGLE了很多,他們已經建議像PDF文件或圖像文件。我需要在頁面上顯示報告。怎麼可以做到這一點?在Asp.Net的網頁中顯示Rdlc報告Mvc4

感謝,

蔡健雅

回答

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更改爲您需要的wath,並記住相應地更改deviceInfo。你可以找到信息here

希望它可以幫助你。

+0

除pdf之外,還有其他選項嗎?我給了MHTML,但它出現錯誤「參數格式超出範圍例外」。 – Tanya

1

這是一個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();