用C#访问SSRS自动导出SSRS报表

时间:2021-10-05 14:28:26

一、              新建一个winform应用程序WindowsFormsApplication1

二、              添加web引用 。

报表服务:http://dbpdhkcax05:80/webservice/ReportService2005.asmx

报表执行服务:http://dbpdhkcax05:80/webservice/ReportExecution2005.asmx

右击“引用”-> 添加服务引用->输入URL地址点“前往”载入服务后点“高级”-服务引用设置->点“添加web引用”->在此画面将2个服务都加入项目。

用C#访问SSRS自动导出SSRS报表

用C#访问SSRS自动导出SSRS报表用C#访问SSRS自动导出SSRS报表用C#访问SSRS自动导出SSRS报表

三、              web服务引用如图示。(web服务引用的节点一开始没有,在添加web服务引用后自动产生).

用C#访问SSRS自动导出SSRS报表

四、              建一form用于输入参数。

用C#访问SSRS自动导出SSRS报表

五、              点OK产生报表(VS2008)

1.插入命名空间引用

 using WindowsFormsApplication1.dbpdhkcax05_rs;
using WindowsFormsApplication1.dbpdhkcax05_rsexec;

2.Click OK 代码

             dbpdhkcax05_rs.ReportingService2005 rs2005 = new ReportingService2005();
dbpdhkcax05_rsexec.ReportExecutionService rsExec = new ReportExecutionService(); rs2005.Credentials = System.Net.CredentialCache.DefaultCredentials;
rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
rsExec.Timeout = -; rs2005.Url = "http://dbpdhkcax05:80/webservice/ReportService2005.asmx?WSDL";
rsExec.Url = "http://dbpdhkcax05:80/webservice/ReportExecution2005.asmx?WSDL"; string reportPath = "/Operation/PortalReports/Reliability Details";
string fileName = @"C:\\testRel.xls"; string format = "EXCEL";
string historyID = null;
string devInfo = null;
bool _forRendering = false; dbpdhkcax05_rs.ParameterValue[] _values = null;
dbpdhkcax05_rs.ReportParameter[] _parm = null;
dbpdhkcax05_rs.DataSourceCredentials[] _cred = null; _parm = rs2005.GetReportParameters(reportPath, historyID,_forRendering, _values, _cred); dbpdhkcax05_rsexec.ExecutionInfo execinfo = rsExec.LoadReport(reportPath, historyID); dbpdhkcax05_rsexec.ParameterValue[] parameters = new WindowsFormsApplication1.dbpdhkcax05_rsexec.ParameterValue[];
// Prepare report parameter.
//ParameterValue[] parameters = new ParameterValue[3];
parameters[] = new dbpdhkcax05_rsexec.ParameterValue();
parameters[].Name = "StartDate";
parameters[].Value = txtFromDate.Text;
parameters[] = new dbpdhkcax05_rsexec.ParameterValue();
parameters[].Name = "EndDate";
parameters[].Value = txtToDate.Text;
parameters[] = new dbpdhkcax05_rsexec.ParameterValue();
parameters[].Name = "Company";
parameters[].Value = txtCompanyId.Text; rsExec.SetExecutionParameters(parameters, "en-us");
Byte[] results;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
string[] streamIDs = null;
dbpdhkcax05_rsexec.Warning[] warning = null;
results = rsExec.Render(format, devInfo, out extension, out mimeType, out encoding, out warning, out streamIDs);
using (FileStream stream = File.OpenWrite(fileName))
{
stream.Write(results, , results.Length); }