Asp.net mvc 从ASP.NET MVC 5内部将C#实体框架数据集绑定到SSR(.rdlc)
Asp.net mvc 从ASP.NET MVC 5内部将C#实体框架数据集绑定到SSR(.rdlc),asp.net-mvc,c#-4.0,ssrs-2008,ssrs-2008-r2,Asp.net Mvc,C# 4.0,Ssrs 2008,Ssrs 2008 R2,//我的控制器。 public ActionResult ExpectedSales(字符串[]季度,整数年,字符串[]部门) { 尝试 { int myQtr=GFIHelper.GetQuarterNumber(季度); 字符串ProductDivisions=Repository.getMyDivision(@User.Identity.Name,Divisions); //所有ReportViewer参数都随GFISSRSReportViewer.GetReportViewer()一起返
//我的控制器。
public ActionResult ExpectedSales(字符串[]季度,整数年,字符串[]部门)
{
尝试
{
int myQtr=GFIHelper.GetQuarterNumber(季度);
字符串ProductDivisions=Repository.getMyDivision(@User.Identity.Name,Divisions);
//所有ReportViewer参数都随GFISSRSReportViewer.GetReportViewer()一起返回;
//.....................................................................
ReportViewer MyReportViewer=GFISSRSReportViewer.GetReportViewer();
MyReportViewer.ServerReport.ReportPath=GFIReportsPath.GetSalesManagementReportURL(“ExpectedSales”);
//.....................................................................
//GFISSRSReportViewer.GetCommonParaList仅包括PrintedDate。
//.....................................................................
List paramList=GFISSRSReportViewer.GetCommonParaList(GFIHelper.TitleCase(User.Identity.Name));
//.....................................................................
字符串nextyear=“”;
如果(myQtr==1)
nextyear=Convert.ToString(年份+0);
其他的
nextyear=Convert.ToString(年份+1);
//必须在此处指定打印人和报告名称
//.....................................................................
string rptname=“预期销售额”+“@”+string.Join(“+”,部门)+“@”+string.Join(“+”,季度)+:“+Year+(Year==Convert.ToInt32(nextyear)?”:“-”+nextyear);
rptname=rptname.Replace(“@”,Environment.NewLine);
添加(新的ReportParameter(“ReportName”,rptname,false));
//.....................................................................
添加(新报告参数(“YearPara”,Convert.ToString(Year),false));
Add(新的ReportParameter(“QuarterStringPara”,Convert.ToString(myQtr),false));
添加(新的ReportParameter(“DivisionsPara”,ProductDivisions,false));
MyReportViewer.ServerReport.SetParameters(paramList);
ViewBag.ReportViewer=MyReportViewer;
返回部分视图(“报告布局”);
}
捕获(例外情况除外)
{
返回PartialView(例如Message+ex.StackTrace);
}
}
//我的报告布局
@使用reportviewvc;
@使用System.Web.UI.WebControl;
报表查看器
@如果(ViewBag.ReportViewer!=null)
{
@Html.ReportViewer(ViewBag.ReportViewer作为Microsoft.Reporting.WebForms.ReportViewer,新
{
frameBorder=“0”,
宽度=单位百分比(100),
高度=600,
滚动条=“是”,
SizeToReportContent=true
})
}
您可以使用在Razor视图中包含ReportViewer
安装软件包后,您可以创建一个视图(例如Report1.cshtml),其外观如下:
@using ReportViewerForMvc;
@if (ViewBag.ReportViewer != null)
{
@Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer)
}
在控制器操作结果中,必须传递ViewBag.ReportViewer
对象,必须实例化该对象并提供数据源
public ActionResult Report1()
{
IEnumerable<Report1DataSet> entities = repo.GetReport1DataSet(); // I am fetching the entities from a repository
ReportViewer rptViewer = new ReportViewer();
rptViewer.ProcessingMode = ProcessingMode.Local;
rptViewer.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\Report1.rdlc";
rptViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", entities));
ViewBag.ReportViewer = rptViewer;
return View();
}
public ActionResult打印报告(int?Id)
{
Profile Profile=db.MyTable.Where(p=>p.Id==Id.FirstOrDefault();
IList reportlist=新列表();
报告列表。添加(概要文件);
LocalReport LocalReport=新建LocalReport();
localReport.ReportPath=Server.MapPath(“~/ReportTemplates/PrintContract.rdlc”);
ReportDataSource ReportDataSource=新的ReportDataSource(“DataSet1”,reportlist);
localReport.DataSources.Add(reportDataSource);
string reportType=“PDF”;//Excel
字符串模拟类型;
字符串编码;
字符串文件名扩展名;
字符串设备信息=
"" +
“PDF”+
//“Excel”+
“10英寸”+
“11英寸”+
“0.5英寸”+
“.4英寸”+
“.1in”+
“0.5英寸”+
"";
警告[]警告;
字符串[]流;
字节[]渲染字节;
//提交报告
renderedBytes=localReport.Render(
报告类型,
deviceInfo,
输出mimeType,
输出编码,
输出文件名扩展名,
流出的溪流,
发出警告);
返回文件(renderdbytes,mimeType);
}
granit,现在我们在MVC控制器rptViewer.LocalReport.DataSources.Add(new ReportDataSource(“Dataset1”,entities))中有了Dataset1,只是想澄清一下报表服务器端的一些问题;ViewBag.ReportViewer=rptViewer;在报表服务器端,我是否必须与DataSet1具有相同的数据集名称,并且报表服务器端的数据源名称应与Stackoverflow.web6.Data相同?以及如何/从何处获得上面的窗口(Dataset属性)@cleaver实际上,如果您像我一样使用ReportViewer,您并不真正需要报表服务器。您只需要.rdlc
。这将在ASP.NET应用程序中处理报告。如果报告不是本地的,则必须在rptViewer.LocalReport
中进行一些修改。数据集名称必须相同,并且当您要在RDLC报表中创建新数据集时,窗口将打开。granit,我非常依赖报表服务器,我们有很多
<package id="MicosoftReportViewerWebForms_v11" version="1.0.1" targetFramework="net452" />
<package id="Microsoft.Report.Viewer" version="11.0.0.0" targetFramework="net452" />
<package id="Microsoft.ReportViewer" version="11.0.3366.16" targetFramework="net452" />
<package id="Microsoft.ReportViewer.2012.Runtime" version="11.0.3452.0" targetFramework="net452" />
<package id="Microsoft.ReportViewer.2015" version="12.0.2.2403" targetFramework="net452" />
<package id="Microsoft.ReportViewer.2015.Runtime" version="12.0.2.2402" targetFramework="net452" />
<package id="Microsoft.ReportViewer.Common" version="10.0.40219.1" targetFramework="net452" />
<package id="Microsoft.ReportViewer.Runtime.Common" version="12.0.2402.15" targetFramework="net452" />
<package id="Microsoft.ReportViewer.Runtime.WebForms" version="12.0.2402.15" targetFramework="net452" />
<package id="Microsoft.ReportViewer.WebForms" version="10.0.40219.1" targetFramework="net452" />
<package id="ReportViewerForMvc" version="1.0.1" targetFramework="net452" />
public ActionResult PrintReport(int? Id)
{
Profile profile = db.MyTable.Where(p => p.Id == Id).FirstOrDefault();
IList<Profile> reportlist = new List<Profile>();
reportlist.Add(profile);
LocalReport localReport = new LocalReport();
localReport.ReportPath = Server.MapPath("~/ReportTemplates/PrintContract.rdlc");
ReportDataSource reportDataSource = new ReportDataSource("DataSet1", reportlist);
localReport.DataSources.Add(reportDataSource);
string reportType = "PDF";//Excel
string mimeType;
string encoding;
string fileNameExtension;
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>PDF</OutputFormat>" +
//" <OutputFormat>Excel</OutputFormat>" +
" <PageWidth>10in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>.4in</MarginLeft>" +
" <MarginRight>.1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
//Render the report
renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
return File(renderedBytes, mimeType);
}