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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C#访问SSRS自动导出SSRS报表相关的知识,希望对你有一定的参考价值。

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

二、              添加web引用 。

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

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

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

技术分享

技术分享技术分享技术分享

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

技术分享

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

技术分享

五、              点OK产生报表(VS2008)

1.插入命名空间引用

技术分享
1 using WindowsFormsApplication1.dbpdhkcax05_rs;
2 using WindowsFormsApplication1.dbpdhkcax05_rsexec;
View Code

2.Click OK 代码

技术分享
 1             dbpdhkcax05_rs.ReportingService2005 rs2005 = new ReportingService2005();
 2             dbpdhkcax05_rsexec.ReportExecutionService rsExec = new ReportExecutionService();
 3 
 4             rs2005.Credentials = System.Net.CredentialCache.DefaultCredentials;
 5             rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
 6             rsExec.Timeout = -1;
 7 
 8             rs2005.Url = "http://dbpdhkcax05:80/webservice/ReportService2005.asmx?WSDL";
 9             rsExec.Url = "http://dbpdhkcax05:80/webservice/ReportExecution2005.asmx?WSDL";
10 
11 
12             string reportPath = "/Operation/PortalReports/Reliability Details";
13             string fileName = @"C:\\testRel.xls";
14 
15             string format = "EXCEL";
16             string historyID = null;
17             string devInfo = null;
18             bool _forRendering = false;
19 
20             dbpdhkcax05_rs.ParameterValue[] _values = null;
21             dbpdhkcax05_rs.ReportParameter[] _parm = null;
22             dbpdhkcax05_rs.DataSourceCredentials[] _cred = null;
23 
24             _parm = rs2005.GetReportParameters(reportPath, historyID,_forRendering, _values, _cred);
25 
26             dbpdhkcax05_rsexec.ExecutionInfo execinfo = rsExec.LoadReport(reportPath, historyID);
27 
28             dbpdhkcax05_rsexec.ParameterValue[] parameters = new WindowsFormsApplication1.dbpdhkcax05_rsexec.ParameterValue[3];
29             // Prepare report parameter.
30             //ParameterValue[] parameters = new ParameterValue[3];
31             parameters[0] = new dbpdhkcax05_rsexec.ParameterValue();
32             parameters[0].Name = "StartDate";
33             parameters[0].Value = txtFromDate.Text;
34             parameters[1] = new dbpdhkcax05_rsexec.ParameterValue();
35             parameters[1].Name = "EndDate";
36             parameters[1].Value = txtToDate.Text; 
37             parameters[2] = new dbpdhkcax05_rsexec.ParameterValue();
38             parameters[2].Name = "Company";
39             parameters[2].Value = txtCompanyId.Text;
40 
41             rsExec.SetExecutionParameters(parameters, "en-us");
42             Byte[] results;
43             string encoding = String.Empty;
44             string mimeType = String.Empty;
45             string extension = String.Empty;
46             string[] streamIDs = null;
47             dbpdhkcax05_rsexec.Warning[] warning = null;
48             results = rsExec.Render(format, devInfo, out extension, out mimeType, out encoding, out warning, out streamIDs);
49             using (FileStream stream = File.OpenWrite(fileName))
50             {
51                 stream.Write(results, 0, results.Length);
52 
53             }
View Code

 

以上是关于用C#访问SSRS自动导出SSRS报表的主要内容,如果未能解决你的问题,请参考以下文章

ssrs 学习连接

ssrs 学习连接

报告正文中的SSRS日历控件

如何为SSRS报表服务器的用户创建常见问题解答?

SSRS-报表生成器-行为列

Excel VBA代码将SSRS数据作为数据集提取