使用程序运行Reporting Service自动生成文件,可以参数使用ReportExecutionService.Render方法进行处理。
1.连接至Reporting Service服务器,生成字符串http://服务器地址/reportserver/reportexecution.asmx,我们在此次测试地址为http://192.168.7.191/reportserver/reportexecution.asmx
2.在VS中添加Web引用填入以上的服务器地址,并添加该引用。
3.加入代码
4.usingSystem;
5.usingSystem.Collections.Generic;
6.usingSystem.Linq;
7.usingSystem.Web;
8.usingSystem.Web.Mvc;
9.usingSSRSGenerate.WebReference;
10.
11.usingSystem.IO;
12.
13.namespaceSSRSGenerate.Controllers
14.{
15.publicclassHomeController:Controller
16.{
17.//
18.// GET:/Home/
19.
20.publicActionResultIndex()
21.{
22.ReportExecutionServicers =newReportExecutionService();
23.rs.Credentials = .CredentialCache.DefaultCredentials;
24.rs.Url ="http://192.168.7.191/reportserver/reportexecution.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 =newParameterValue[3];
35.parameters[0] =newParameterValue();
36.parameters[0].Name ="EmpID";
37.parameters[0].Value ="288";
38.parameters[1] =newParameterValue();
39.parameters[1].Name ="ReportMonth";
40.parameters[1].Value ="6";// June
41.parameters[2] =newParameterValue();
42.parameters[2].Name ="ReportYear";
43.parameters[2].Value ="";
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 =newExecutionInfo();
55.ExecutionHeaderexecHeader =newExecutionHeader();
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,outextension,outencoding,outmimeType,outwarnings,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(Exceptione)
91.{
92.Console.WriteLine(e.Message);
93.}
97.returnView();
98.}
99.
100.}
101.}
102.