coy 的程序人生

有鸟止南方之阜,三年不翅,不飞不鸣
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

通过编程方式将Reporting Services 报表导出成Excel文件

Posted on 2007-11-25 13:16  coy  阅读(3036)  评论(8编辑  收藏  举报
Reporting Services的报表做好后,可以在查看报表时选择导出成EXCEL。这是Reporting Services自带的功能。
但是,这种方法必须是在查看报表时才能使用。有时候我不希望让用户进入查看报表的页面(觉得麻烦),或者我想一批自动生成很多报表的EXCEL文件供下载。这就只能通过编程方式实现了。
目的:连接某个报表,传入相关参数,直接生成EXCEL文件,放在WEB服务器上(注意不是Reporting Services的服务器)。
我写了一个类RSReport用来实现这个功能。
这里必须使用Reporting Services提供的一个Web Service,所以要加一个Web Reference: http://servername/ReportServer/ReportService.asmx
假设使用默认的命名空间WebReference。
下面的代码中:
_reportPath是报表路径如:/foldername/myreport1
_parameters是报表参数列表
_savePath是指定的EXCEL文件存放路径,是全路径名。如d:\myproject\tempfiles\report1.xls。
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Web.Services.Protocols;
using coysoft.WebReference;
using System.IO;

namespace coysoft.Reporting
{
    
public class RSReport
    
{
        
private string _reportPath;
        
private Hashtable _parameters = new Hashtable();
        
private string _renderFormat = "EXCEL";
        
private string _savePath;

        
public string SavePath
        
{
            
get return _savePath; }
            
set { _savePath = value; }
        }


        
public string RenderFormat
        
{
            
get return _renderFormat; }
            
set { _renderFormat = value; }
        }


        
public string ReportPath
        
{
            
get return _reportPath; }
            
set { _reportPath = value; }
        }


        
public void AddParameter(string parmKey, string parmValue)
        
{
            _parameters.Add(parmKey, parmValue);
        }


        
public void Render()
        
{
            ReportingService rs 
= new ReportingService();
            rs.Credentials 
= System.Net.CredentialCache.DefaultCredentials;
             rs.Timeout = 600000; //设置过期时间为10分钟

            
// Render arguments
            byte[] result = null;
            
string reportPath = _reportPath;
            
string format = _renderFormat;
            
string historyID = null;
            
string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

            
// Prepare report parameter.
            ParameterValue[] parameters = new ParameterValue[_parameters.Count];
            
int i = 0;
            
foreach(string key in _parameters.Keys)
            
{
                parameters[i] 
= new ParameterValue();
                parameters[i].Name 
= key;
                parameters[i].Value 
= Convert.ToString(_parameters[key]);
                i
++;
            }

            
            DataSourceCredentials[] credentials 
= null;
            
string showHideToggle = null;
            
string encoding;
            
string mimeType;
            Warning[] warnings 
= null;
            ParameterValue[] reportHistoryParameters 
= null;
            
string[] streamIDs = null;
            SessionHeader sh 
= new SessionHeader();
            rs.SessionHeaderValue 
= sh;

            
try
            
{
                result 
= rs.Render(reportPath, format, historyID, devInfo, parameters, credentials,
                   showHideToggle, 
out encoding, out mimeType, out reportHistoryParameters, out warnings,
                   
out streamIDs);
                sh.SessionId 
= rs.SessionHeaderValue.SessionId;
            }

            
catch (SoapException e)
            
{
                
throw e;
            }

            
// Write the contents of the report to an excel file.
            try
            
{
                FileStream stream 
= new FileStream(_savePath, FileMode.Create);
                stream.Write(result, 
0, result.Length);
                stream.Close();
            }

            
catch (Exception e)
            
{
                
throw e;
            }

        }

    }

}