使用程序运行Reporting Service自动生成文件,可以参数使用ReportExecutionService.Render方法进行处理。
1. 连接至Reporting Service服务器,生成字符串http://服务器地址/reportserver/reportexecution2005.asmx,我们在此次测试地址为http://192.168.7.191/reportserver/reportexecution2005.asmx
2. 在VS中添加Web引用填入以上的服务器地址,并添加该引用。
3. 加入代码
4. using System;
5. using System.Collections.Generic;
6. using System.Linq;
7. using System.Web;
8. using System.Web.Mvc;
9. using SSRSGenerate.WebReference;
10.
11.using System.IO;
12.
13.namespace SSRSGenerate.Controllers
14.{
15. public class HomeController: Controller
16. {
17. //
18. // GET:/Home/
19.
20. public ActionResult Index()
21. {
22. ReportExecutionServicers = new ReportExecutionService();
23. rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
24. rs.Url = "http://192.168.7.191/reportserver/reportexecution2005.asmx";
25.
26. // Renderarguments
27. byte[]result = null;
28. stringreportPath = "/PPO/KnitPPO_COMPARE/PPO_Compare_Bulk";
29. stringformat = "MHTML";//也可以导出PDF
30. stringhistoryID = null;
31. stringdevInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
32.
33. //Prepare report parameter.
34. ParameterValue[]parameters = new ParameterValue[3];
35. parameters[0] = new ParameterValue();
36. parameters[0].Name = "EmpID";
37. parameters[0].Value = "288";
38. parameters[1] = new ParameterValue();
39. parameters[1].Name = "ReportMonth";
40. parameters[1].Value = "6"; // June
41. parameters[2] = new ParameterValue();
42. parameters[2].Name = "ReportYear";
43. parameters[2].Value = "2004";
44.
45. DataSourceCredentials[]credentials = null;
46. stringshowHideToggle = null;
47. stringencoding;
48. stringmimeType;
49. stringextension;
50. Warning[]warnings = null;
51. ParameterValue[]reportHistoryParameters = null;
52. string[]streamIDs = null;
54. ExecutionInfoexecInfo = new ExecutionInfo();
55. ExecutionHeaderexecHeader = new ExecutionHeader();
56.
57. rs.ExecutionHeaderValue =execHeader;
58.
59. execInfo = rs.LoadReport(reportPath,historyID);
60.
61. rs.SetExecutionParameters(parameters,"en-us");
62. StringSessionId = rs.ExecutionHeaderValue.ExecutionID;
63.
64. Console.WriteLine("SessionID: {0}",rs.ExecutionHeaderValue.ExecutionID);
65.
67. try
68. {
69. result = rs.Render(format,devInfo, out extension, out encoding, out mimeType, out warnings,outstreamIDs);
70.
71. execInfo =rs.GetExecutionInfo();
73. Console.WriteLine("Execution date and time: {0}",execInfo.ExecutionDateTime);
76. }
77. catch(SoapException e)
78. {
79. Console.WriteLine(e.Detail.OuterXml);
80. }
81. // Writethe contents of the report to an MHTML file.
82. try
83. {
84. FileStreamstream = File.Create("report.mht",result.Length);
85. Console.WriteLine("File created.");
86. stream.Write(result, 0, result.Length);
87. Console.WriteLine("Result written to the file.");
88. stream.Close();
89. }
90. catch(Exception e)
91. {
92. Console.WriteLine(e.Message);
93. }
97. returnView();
98. }
99.
100. }
101. }
102.