Using two WebServices exposed by SSRS to create PDF report formats programmatically

来源:互联网 发布:C语言cost 编辑:程序博客网 时间:2024/05/20 23:40

I had a requirement to automate the generation of PDF files of SSRS reports.

Figured out a way to use 2 webservices exposed by SQL Server Reporting Services 2005:
a. ReportService2005
b. ReportExecution2005

Implementation:
a. Create SSRS reports and deploy it on the report server.
b. In the windows application add reference to the 2 SSRS webservices: ReportService2005, ReportExecution2005
c. Set default report credentials.
d. Set 2 webservies url
e. Load the SSRS report using ReportExecution2005
f. Get SSRS reportparameters using ReportService2005, to get the total parameters in the report and set the appropriate parameters value to render the report using ReportExecution2005. (confused !!!. Check the code below)
g. Set report parameters using SetExecutionParameters() method of ReportExecution2005.
h. Render the report to get the PDF in byteArray format.
i. Write the pdf byteArray[] to the given pdf file using FileStream Class.

 

  1. static private void CreatePDFReports()
  2. {
  3. //create proxy to the webservice
  4. ReportService2005.ReportingService2005 rs2005 = new ReportService2005.ReportingService2005();
  5. ReportExecution2005.ReportExecutionService re2005 = new ReportExecution2005.ReportExecutionService();
  6. try
  7. {
  8. //authenticate to the web service using windows credentials
  9. rs2005.Credentials = System.Net.CredentialCache.DefaultCredentials;
  10. re2005.Credentials = System.Net.CredentialCache.DefaultCredentials;
  11. rs2005.Url = ConfigurationSettings.AppSettings["ReportService2005.ReportService2005"];
  12. re2005.Url = ConfigurationSettings.AppSettings["ReportExecution2005.ReportExecution2005"];
  13. //prepare render arguments
  14. string historyID = null;
  15. string deviceinfo = null;
  16. string format = "PDF";
  17. byte[] bytPDF;
  18. string encoding = string.Empty;
  19. string mimeType = string.Empty;
  20. string extension = string.Empty;
  21. ReportExecution2005.Warning[] warnings = null;
  22. string[] streamIDs = null;
  23. //define variables needed for GetParameters() method
  24. //get the report name
  25. string _reportname = ConfigurationSettings.AppSettings["reportName"];
  26. string _historyID = null;
  27. bool _forRendering = false;
  28. ReportService2005.ParameterValue[] _values = null;
  29. ReportService2005.DataSourceCredentials[] _credentials = null;
  30. ReportService2005.ReportParameter[] _parameters = null;
  31. //load the selected report.
  32. ReportExecution2005.ExecutionInfo ei = re2005.LoadReport(_reportname, historyID);
  33. //Get the no. of parameters in the report.
  34. _parameters = rs2005.GetReportParameters(_reportname, _historyID, _forRendering, _values, _credentials);
  35. int totalParams = _parameters.Length;
  36. ReportExecution2005.ParameterValue[] parameters = null;
  37. //prepare report parameters
  38. parameters = new ReportExecution2005.ParameterValue[totalParams];
  39. foreach (ReportService2005.ReportParameter rp in _parameters)
  40. {
  41. switch (rp.Name)
  42. {
  43. case "param1_name":
  44. {
  45. parameters[_parameters.Length - totalParams] = new scheduleReports.ReportExecution2005.ParameterValue();
  46. parameters[_parameters.Length - totalParams].Name = rp.Name;
  47. parameters[_parameters.Length - totalParams].Value = 1;
  48. }
  49. break;
  50. case "param2_name":
  51. {
  52. parameters[_parameters.Length - totalParams] = new scheduleReports.ReportExecution2005.ParameterValue();
  53. parameters[_parameters.Length - totalParams].Name = rp.Name;
  54. parameters[_parameters.Length - totalParams].Value = 100;
  55. }
  56. break;
  57. }
  58. totalParams--;
  59. }
  60. re2005.SetExecutionParameters(parameters, "en-us");
  61. bytPDF = re2005.Render(format, deviceinfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
  62. FileStream fs = new FileStream("report.pdf", FileMode.OpenOrCreate, FileAccess.Write);
  63. fs.Write(bytPDF, 0, bytPDF.Length);
  64. fs.Close();
  65. }
  66. catch (Exception ex)
  67. {
  68. Console.WriteLine(ex.Message);
  69. Console.Read();
  70. }
原创粉丝点击