Wednesday, April 17, 2013

SSRS report to PDF saved in hard drive folder

This code executes an SSRS report located on a remote server, then serializes the report as PDF to a folder on the hard drive:

You have to create a web reference called ReportServiceReference, and import the url  http://bidev.develop.fcbt/ReportServer/ReportExecution2005.asmx?wsdl

This is the report I will be running.


 

using System;
using System.IO;
using System.Net;
using Applications.LoanAccounting.BillsAndStatements.Properties;
using Applications.LoanAccounting.BillsAndStatements.ReportServiceReference;

namespace Applications.LoanAccounting.BillsAndStatements
{
    public class DocumentRenderingManager
    {
        #region Private members

        private ReportExecutionService _reportExecutionService;
        #endregion


        #region Constructor
        public DocumentRenderingManager()
        {
            _reportExecutionService = new ReportExecutionService();
            _reportExecutionService.Credentials = CredentialCache.DefaultCredentials;
        }

        #endregion

        #region General public methods

        public string RunBillingReportByHeaderId(string headerId)
        {

            Guid id = Guid.NewGuid();

            string fileName = String.Format("{0}{1}{2}.pdf",
                (string)Settings.Default["PDFGenerationLocation"],
                "BillReport",
                id);

            string _reportName = (string)Settings.Default["BillingReportFolderName"] +
                (string)Settings.Default["BillingReportName"];

            string deviceInfo = null;
            const string format = "PDF";
            Byte[] results;
            string encoding = String.Empty;
            string mimeType = String.Empty;
            string extension = String.Empty;
            Warning[] warnings = null;
            string[] streamIDs = null;

            try
            {
                ExecutionInfo ei = _reportExecutionService.LoadReport(_reportName, null);
                ParameterValue[] parameters = new ParameterValue[1];
                parameters[0] = new ParameterValue();
                parameters[0].Label = "BillHeaderId";
                parameters[0].Name = "BillHeaderId";
                parameters[0].Value = headerId;
                _reportExecutionService.SetExecutionParameters(parameters, "en-us");

                results = _reportExecutionService.Render(format,
                              deviceInfo,
                              out extension,
                              out encoding,
                              out mimeType,
                              out warnings,
                              out streamIDs);

                using (FileStream stream = File.OpenWrite(fileName))
                {
                    stream.Write(results, 0, results.Length);
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return fileName;
        }
        #endregion
    }
}

No comments:

Post a Comment