[英]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.