环境 ASP.NET MVC 4.0 SQL Server Reporting Services 需求 在保存报表为文件(如PDF)之前,可以预览报表(支持图片)。 分析 网络上的解决方案,都是告诉你用最原始的办法:结合ASP.NET Web Form Report Viewer控件。因为SQL Server Reporting Services (SSRS) 只有Web Form的Report Viewer控件,没对ASP.NET MVC进行特别支持。 我们不能直接在ASP.NET MVC用Report Viewer是因为Report Viewer依赖View State,而View State正是MVC要解决的问题。。。 因为不满意这种整合Web Form的解决方案,今晚思考了一下,既然SSRS支持渲染结果为MHTML(MIME HTML),那么,如果我们解决浏览器不显示MHTML内容(需要另存再本地打开),就可以实现预览的办法:先渲染成MHTML,再解释MIME,再把图片附件转成data嵌入。 步骤 1. 设计页面因为预览结果是完整的HTML,我在报表页面嵌入了一个iframe。。。。。。src指向一个Web API,该Web API会返回渲染后的html内容(包括图片)。 2. 下载文件添加一个Web API 函数,用以渲染MHTML并返回text/html结果。为了简化操作,我把报表参数拼接起来,键值用“|”分隔,参数用“`”分隔。实际使用请根据自己的情况修改。。。 publicHttpResponseMessage DownloadReportContent(stringReportFileName, stringParameters) { varparameters = newDictionary<string, string>(); Parameters.Split('`').ForEach(p => { varparts = p.Split('|'); parameters.Add(parts[0], parts[1]); }); byte[] reportContent; stringfileExtension, mimeType; ReportGenerator.GenerateReport('ReportServer', 'ReportServerExecutionURL', 'ReportServerUserName', 'ReportServerPassword', 'ReportServerDomain', ReportFileName, ExportFormat.MHTML, parameters, string.Empty, outreportContent, outfileExtension, outmimeType); reportContent = ReportConvertor.Convert(reportContent); varresultStream = newSystem.IO.MemoryStream(reportContent); resultStream.Position = 0; varresponse = newHttpResponseMessage(); response.StatusCode = HttpStatusCode.OK; response.Content = newStreamContent(resultStream); returnresponse; } 3. 渲染报表为MHTML下面的方法可以用作渲染其它格式,如最常见的PDF usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Security.Principal; //using YOUR_IMPORTED_WEB_REFERENCE_FOR_SSRS_EXECUTION_SERVICE_HERE namespaceOrg.SSRSPreview { /// <summary> /// Export Formats /// </summary> publicenumExportFormat { /// <summary>XML</summary> XML, /// <summary>Comma Delimitted File CSV, /// <summary>TIFF image</summary> Image, /// <summary>PDF</summary> PDF, /// <summary>HTML (Web Archive)</summary> MHTML, /// <summary>HTML 4.0</summary> HTML4, /// <summary>HTML 3.2</summary> HTML32, /// <summary>Excel</summary> Excel, /// <summary>Word</summary> Word } publicclassReportGenerator { /// <summary> /// Gets the string export format of the specified enum. /// </summary> /// <param name='Format'>export format enum</param> /// <returns>enum equivalent string export format</returns> privatestringGetExportFormatString(ExportFormat Format) { switch(Format) { caseExportFormat.XML: return'XML'; caseExportFormat.CSV: return'CSV'; caseExportFormat.Image: return'IMAGE'; caseExportFormat.PDF: return'PDF'; caseExportFormat.MHTML: return'MHTML'; caseExportFormat.HTML4: return'HTML4.0'; caseExportFormat.HTML32: return'HTML3.2'; caseExportFormat.Excel: return'EXCEL'; caseExportFormat.Word: return'WORD'; default: return'PDF'; } } /// <summary> /// generate a report /// </summary> /// <param name='ReportServer'>report server</param> /// <param name='ReportServerExecutionURL'>rendering execution url of report server</param> /// <param name='ReportServerUserName'>user name to access report server</param> /// <param name='ReportServerPassword'>password of the user name</param> /// <param name='ReportServerDomain'>domain of the report server (for active directory)</param> /// <param name='ReportServerSubDir'>folder of the report in the report server</param> /// <param name='FileFormat'>output of the report</param> /// <param name='Parameters'>parameters for the report</param> /// <param name='ReportContent'>rendered report content</param> /// <param name='FileExtension'>file extension of the report</param> /// <param name='MimeType'>mime type of the generated file</param> publicvoidGenerateReport(stringReportServer, stringReportServerExecutionURL, stringReportServerUserName, stringReportServerPassword, stringReportServerDomain, stringReportServerSubDir, stringReportFileName, ExportFormat FileFormat, Dictionary<string, string> Parameters, stringDeviceInfo, outbyte[] ReportContent, outstringFileExtension, outstringMimeType) { using(varreportExecutionService = newReportExecutionService.ReportExecutionServiceSoapClient('ReportExecutionServiceSoap', ReportServerExecutionURL)) { reportExecutionService.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation; reportExecutionService.ClientCredentials.UserName.UserName = ReportServerUserName; reportExecutionService.ClientCredentials.UserName.Password = ReportServerPassword; reportExecutionService.ClientCredentials.Windows.ClientCredential = newSystem.Net.NetworkCredential(ReportServerUserName, ReportServerPassword, ReportServerDomain); varparameters = Parameters.Select(p => newParameterValue { Name = p.Key, Value = p.Value }).ToList(); // Init Report to execute ServerInfoHeader serverInfoHeader; ExecutionInfo executionInfo; ExecutionHeader executionHeader = reportExecutionService.LoadReport(null, ReportFileName, null, outserverInfoHeader, outexecutionInfo); // Attach Report Parameters reportExecutionService.SetExecutionParameters(executionHeader, null, parameters.ToArray(), null, outexecutionInfo); // Render stringencoding; Warning[] warnings; string[] streamIds; reportExecutionService.Render(executionHeader, null, GetExportFormatString(FileFormat), DeviceInfo, outReportContent, outFileExtension, outMimeType, outencoding, outwarnings, outstreamIds); } } } } |
|
来自: DiberyChen > 《待分类》