public static class HotelReport { public static byte[] GenerateReport(string dtatSetName, IEnumerable dataSource,
string reportFilePath,string reportType, out string mimeType,
out string encoding, out string fileNameExtension) { //報(bào)表數(shù)據(jù)源 ReportDataSource reportDataSource = new ReportDataSource(dtatSetName, dataSource); //本地化報(bào)表 LocalReport localReport = new LocalReport(); localReport.ReportPath = reportFilePath; localReport.DataSources.Add(reportDataSource); string deviceInfo = "<DeviceInfo>" + " <OutputFormat>" + reportType + "</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; renderedBytes = localReport.Render( reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); return renderedBytes; } }
上面是用于將RDLC報(bào)表轉(zhuǎn)換為二進(jìn)制數(shù)據(jù)的方法。
下面的ReporesRelsult用于將這種數(shù)據(jù)寫(xiě)到輸出當(dāng)中
public class ReportsResult : ActionResult { public ReportsResult(byte[] data, string mineType) { this.Data = data; this.MineType = mineType; } public byte[] Data { get; set; } public string MineType { get; set; } public override void ExecuteResult(ControllerContext context) { if (Data == null) { new EmptyResult().ExecuteResult(context); return; } context.HttpContext.Response.ContentType = MineType; using (MemoryStream ms = new MemoryStream(Data)) { ms.Position = 0; using (StreamReader sr = new StreamReader(ms)) { context.HttpContext.Response.Output.Write(sr.ReadToEnd()); } } } }
下面是控制器的action方法:
public ActionResult EmployeesNumberPerYear() { string dtatSetName = "DsENPerYear"; var dataSource = EmployeeReports.EmployeesNumberPerYear(employeeRepository); string reportFilePath = Server.MapPath("~/RDLC/Employee/EmployeesNumberPerYear.rdlc"); string reportType = "PDF"; string mimeType; string encoding; string fileNameExtension; byte[] renderedBytes = HotelReport.GenerateReport(dtatSetName, dataSource, reportFilePath, reportType, out mimeType, out encoding, out fileNameExtension); return new ReportsResult(renderedBytes, mimeType); }
以及html的調(diào)用:
<div> @{ Html.RenderAction("EmployeesNumberPerYear", "EmployeeReports"); }</div>
其實(shí)問(wèn)題就在于擴(kuò)展的ActionResult,怎樣將報(bào)表生成的RDLC轉(zhuǎn)換過(guò)的二進(jìn)制數(shù)據(jù)通過(guò)HttpContext來(lái)進(jìn)行輸出,并且RenderAction能夠正確的顯示出預(yù)覽。
另外要說(shuō)明的是,如果直接通過(guò)連接或url訪問(wèn)該action,excel的格式會(huì)直接用于保存,pdf的會(huì)顯示出來(lái)。但我想讓數(shù)據(jù)以預(yù)覽的形式展示到div當(dāng)中。
- 1 回答
- 0 關(guān)注
- 680 瀏覽
添加回答
舉報(bào)
0/150
提交
取消