SQL Server 报告压缩解决方案(Zip 格式)






2.13/5 (8投票s)
2005 年 3 月 14 日
3分钟阅读

43426
压缩大型 SQL Server 报表,并允许用户以 ZIP 格式下载。
引言
Microsoft SQL Server 报表服务是新兴的报表技术。由于报表服务与 MS SQL Server 一起发布,几乎所有基于 .NET 的项目都使用它。
报表通常用于管理层的决策,因此在显示结果时必须非常快。如果要显示的数据量很大,而网络带宽又很小,该怎么办?结果是,这将花费更多时间,从而导致报表服务超时错误。为了避免这些错误并赢得客户满意度,我们可以动态地将报表压缩为 ZIP 文件格式,并允许用户将其下载到本地硬盘驱动器中。可以使用 C# 或 VB.NET 以编程方式实现上述解决方案。
注意:可以压缩任何扩展名(.xls、.pdf、.html 等)的报表。
本文中介绍的解决方案使用以 C# 语言编写的示例代码片段。
要求
- SharpZipLib:一个免费的开源 .NET ZIP 库(下载最新版本)。
- 对应用程序所在的服务器上的项目Temp文件夹的完全控制访问权限。
分步过程
只有当报表中的数据量很大时,才能使用下面解释的过程
- 通过报表 Web 服务获取字节数组形式的报表数据。
- 检查字节数组的长度,如果超过指定的限制,我们可以压缩报表,否则保持默认设置。
- 指定要在Temp文件夹中保存的报表的文件名和扩展名。
- 在Temp文件夹中创建报表文件。
- 压缩保存在Temp文件夹中的报表,并删除报表文件。
- 允许用户通过浏览器下载保存的 Zip 文件。
通过单独的线程定期清空Temp文件夹。
代码摘要
以下代码处理通过 SQL Server 报表服务获取字节数组形式的报表
ReportingService objReportService = new ReportService.ReportingService();
NetworkCredential objCredentials = new NetworkCredential("","","");
objReportService.Credentials = objCredentials;
string strReportFolderName = "" // Report folder name;
string strDSNPath = "" // Report DSN path ;
string strDataSourceName = "" // DataSourceName;
ReportService.DataSourceReference objDataSourceReference =
new ReportService.DataSourceReference();
objDataSourceReference.Reference = "\"+ strDSNPath;
objArrDataSources = new ReportService.DataSource[1];
objDataSources = new ReportService.DataSource();
objDataSources.Item =
(ReportService.DataSourceDefinitionOrReference) objDataSourceReference;
objDataSources.Name = strDataSourceName;
objArrDataSources[0] = objDataSources;
objReportService.SetReportDataSources(strReportName.Trim(), objArrDataSources);
//Device Information settings....Section attribute
// made to zero to show all the results.
//if this is made to 1 shows the first report only.
strDeviceInfo=
"<DeviceInfo><HTMLFragment>True</HTMLFragment>" +
"<Section>0</Section></DeviceInfo>";
//Passing parameters to report
arrParameters = new ReportService.ParameterValue[intHTCount];
if (objHTParameters !=null)
{
intHTCount=objHTParameters.Count;
arrParameters = new ReportService.ParameterValue[intHTCount];
IDictionaryEnumerator enumParameterList = objHTParameters.GetEnumerator();
while ( enumParameterList.MoveNext() )
{
arrParameters[intParameterCount]=new ReportService.ParameterValue();
arrParameters[intParameterCount].Name =
enumParameterList.Key.ToString();
arrParameters[intParameterCount].Value =
enumParameterList.Value.ToString();
intParameterCount=intParameterCount+1;
}
}
objReportService.Timeout = -1; //Getting report from web service as bytes.
byteResultStream = objReportService.Render(strReportName, strReportType,
strHistoryID,strDeviceInfo,arrParameters,
objDataSourceCredentials,strShowHideToggle,
out strEncoding,out strMimeType,out objParametersUsed,
out objWarnings,out strArrStreamIdentifiers);
私有方法
建议的解决方案涉及两个 private
方法,下面将详细讨论这些方法。
WriteFile
:此方法用于将报表写入Temp文件夹。以下是相同的代码/*Parameters: strpath – A string which holds the path where the report files has to be saved. strFileNameWithExt – A string which holds the name of report file with extension (.html, .pdf, .xls etc). byteResultStream – A byte array which holds the data of the report in the form of bytes. Method:*/ private int WriteFile (string strPath, string strFileNameWithExt,byte[] byteResultStream) { int intResult = 0; FileStream stream = File.OpenWrite(@strPath+strFileNameWithExt); stream.Write(byteResultStream, 0, byteResultStream.Length); stream.Close(); intResult = ”1”; return intResult; #endregion }
CreateZip
:此方法用于压缩保存的报表文件。该代码可在与库一起下载的示例之一中找到。我已经根据我的要求对其进行了自定义/*Parameters: strFileName – A string which holds the filename without the extension strFileNameWithExt – A string which holds the filename with extension Method :*/ private string CreateZip(string strFileName,string strFileNameWithExt) { string strZipFileName = string.Empty; Crc32 objCrc32 = null; ZipOutputStream objZipOutputStream = null; ZipEntry objZipEntry = null; string strPath = string.Empty; #endregion strPath = HttpContext.Current.Server.MapPath(“\Temp”); if (File.Exists(strPath+strFileNameWithExt)) { strZipFileName = strPath+strFileName+”.zip”; objCrc32 = new Crc32(); objZipOutputStream = new ZipOutputStream(File.Create(strZipFileName)); objZipOutputStream.SetLevel(6); FileStream objFileStream = File.OpenRead(strPath+strFileNameWithExt); Byte[] abyBuffer = new Byte[objFileStream.Length]; objFileStream.Read(abyBuffer, 0, abyBuffer.Length); objZipEntry = new ZipEntry(strFileNameWithExt); objZipEntry.DateTime = DateTime.Now; objZipEntry.Size = objFileStream.Length; objFileStream.Close(); objCrc32.Reset(); objCrc32.Update(abyBuffer); objZipEntry.Crc = objCrc32.Value; objZipOutputStream.PutNextEntry(objZipEntry); objZipOutputStream.Write(abyBuffer, 0, abyBuffer.Length); objZipOutputStream.Finish(); objZipOutputStream.Close(); strZipFileName = strFileName+”.zip”; return strZipFileName; } }
待办
- 在上面的代码片段中,我没有做任何异常处理,但在实际场景中这是必须的。
- 上面的代码是为我的项目需求而编写的,您可以根据您的要求对其进行自定义。
- Temp文件夹中保存的 zip 文件应通过计划程序或程序定期删除。
结论
我希望这篇文章对大家有帮助。每当遇到有趣和有用的东西时,我都会写更多的文章。
摘要
本文档介绍了解决 SQL Server 报表服务超时问题的一种方法,可以查看包含大量数据的报表。该解决方案是将报表压缩为 Zip 格式,并允许用户下载该文件。
编程愉快。