ASP.NET MVC + Microsoft.Reporting.WebForms.ReportViewer 方法
Download ( Microsoft.Reporting.WebForms ) from NuGet. |
Server Side – MVC Controller using Microsoft.Reporting.WebForms; // Action Result to Result Report File in PDF Format public ActionResult DownloadPDFwithParameters(string reportName, string fileName, string format = "PDF") { ReportViewer reportViewer = new ReportViewer(); Warning[] warnings; string[] streamIds; string mimeType = string.Empty; string encoding = System.Text.Encoding.UTF8.ToString(); string extension = string.Empty; ReportViewer viewer = new ReportViewer(); viewer.ProcessingMode = ProcessingMode.Local; viewer.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"\Report\" + reportName; viewer.LocalReport.SubreportProcessing += LocalReport_SubreportProcessing; List<ReportDataSource> datasources = this.getDateSource(reportName); foreach (ReportDataSource ds in datasources) viewer.LocalReport.DataSources.Add(ds); viewer.LocalReport.SetParameters(getParameters(reportName)); byte[] bytes = viewer.LocalReport.Render(format, null, out mimeType, out encoding, out extension, out streamIds, out warnings); string filepath = AppDomain.CurrentDomain.BaseDirectory + fileName; string contentType = MimeMapping.GetMimeMapping(filepath); return ReturnFileContent(bytes, fileName, contentType, false); } // Event Handler to assign SubReport DataSet private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e) { switch (e.ReportPath) { case "Log4Net.rdlc": e.DataSources.Add(new ReportDataSource("DataSet_Case", … … )); break; default: break; } } // Function to Return Report DataSet public List<ReportDataSource> getDateSource(string reportName) { List<ReportDataSource> datasources = new List<ReportDataSource>(); switch (reportName) { case "Log4Net.rdlc": datasources.Add(new ReportDataSource("DataSet_Log4Net", this.context.Log4Net.ToList())); break; default: break; } return datasources; } // Function to Return Report Parameter public List<ReportParameter> getParameters(string reportName) { List<ReportParameter> parameters = new List<ReportParameter>(); switch (reportName) { case "Log4Net.rdlc": parameters.Add(new ReportParameter("Parameter", … … )); break; default: break; } return parameters; } // Return PDF File from ASP.NET MVC Controller – File Result. public FileContentResult ReturnFileContent(byte[] fileContent, String filename = "", String contentType = "", bool inline = true) { var cd = new System.Net.Mime.ContentDisposition { FileName = filename, Inline = false, }; Response.AppendHeader("Content-Disposition", cd.ToString()); Response.ContentType = contentType; return File(fileContent, contentType); } |
Client Side – JavaScript $("#download").click(function () { window.location = "@(Url.Action("DownloadPDFwithParameters", new { reportName = "Log4Net.rdlc", fileName = "Log4Net.pdf" }))"; return false; }); |
When the Report is started to be Developed from Visual Studio, you can add the Report in rdl Format ( need to update XML Content from the file to be rdlc ) or in rdlc Format from the Project. When the SQL Statement – DataSet is defined from the Report & Report Wizard, it would not be used from the Web Application finally. Only Model of SQL Statement would be used from Code. ( Example : SELECT " AS ID, " AS Name, " AS PostTitle FROM TBL ) You need to bind the Object Collection List<…> with the above Model on DataSet from Code. public class TBL { public long ID { get; set; } public string Name { get; set; } public string PostTitle { get; set; } } Reference From Existing Framework Code Practice. |