65.9K
CodeProject 正在变化。 阅读更多。
Home

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

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.10/5 (5投票s)

2005 年 3 月 14 日

3分钟阅读

viewsIcon

39407

压缩 SQL Server 报表,允许用户以 Zip 格式下载报表。

引言

Microsoft SQL Server Reporting Services 是新兴的报表服务技术。 随着报表服务与 MS SQL Server 一起发布,现在在每个基于 .NET 的项目中都经常使用它。

报表通常用于管理中的决策目的,并且在显示结果时必须快速。 如果要显示的数据量很大,而网络带宽很小怎么办? 结果是,这将花费更多时间,进而导致报表服务的超时错误。 为了避免这些类型的错误并赢得客户满意度,我们可以动态地将报表压缩为 ZIP 文件格式,并允许用户将文件下载到他们的本地硬盘中。 可以使用 C# 或 VB.NET 以编程方式实现上述解决方案。

注意: 任何扩展名 (.xls, .pdf, .html 等) 的报表都可以被压缩。

本文中涵盖的解决方案使用使用 ASP.NET 和 C# 开发的 Web 应用程序项目。

要求

  • SharpZipLib – 免费开源 .NET zip 库。(下载 URL:ic#code)。
  • 完全控制应用程序所在服务器中项目的 Temp 文件夹的访问权限。

逐步程序

只有当报表中的数据量很大时,才能使用下面解释的程序。

  • 通过报表 Web 服务以字节数组的形式获取报表数据。
  • 检查字节数组的长度,如果它超过指定的限制,我们可以压缩报表,否则将其保留为默认值。
  • 指定文件名和扩展名,以便将报表保存在 Temp 文件夹中。
  • Temp 文件夹中创建报表文件。
  • 压缩保存在 Temp 文件夹中的报表,并删除报表文件。
  • 允许用户通过浏览器下载保存的 Zip 文件。
  • 通过单独的线程定期清空 Temp 文件夹。

代码摘要

以下代码处理通过 SQL Server 报表服务以字节数组的形式获取报表。

ReportingService objReportService = 
                           new ReportService.ReportingService();
NetworkCredential objCredentials = new NetworkCredential("","","");
objReportService.Credentials = objCredentials;
string strDSNPath = "" // Report DSN path ;
string strReportFolderName = "" // Report folder name;
string strDataSourceName  = "" // DataSourceName;
ReportService.DataSourceReference objDataSourceReference = 
                            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)
    {
      I 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 格式,并允许用户下载 Zip 文件。

结论

我希望这篇文章对大家有帮助。 每当我遇到有趣且有用的东西时,我都会更新更多文章。

编程愉快。

© . All rights reserved.