Programmatically Export from SQL Reporting Services in C#

Here is the sample code for rendering SQL Reporting Services Report to desired file types (PDF, XLS etc.):

private void generateReport(string _ReportName,string _period1,string _period2,string _division,string _department,string _employee)

{

try

{

string _ReportServer = System.Configuration.ConfigurationManager.ConnectionStrings["ReportServer"].ToString();

string _ReportPath = System.Configuration.ConfigurationManager.ConnectionStrings["ReportPath"].ToString();

ReportViewer1.ProcessingMode = ProcessingMode.Remote;

Uri ReportURI = new Uri(_ReportServer);

this.ReportViewer1.ServerReport.ReportServerUrl = ReportURI;

this.ReportViewer1.ServerReport.ReportPath = _ReportPath + _ReportName;

int repParams = 2;

switch (_ReportName)

{

case "/ExpenseReportByDivision":

repParams = 2;

break;

case "/ExpenseReportByDepartment":

repParams = 3;

break;

case "/ExpenseReportByEmployee":

repParams = 4;

break;

case "/CallDetailsByAccount":

repParams = 5;

break;

}

ReportParameter[] parameters = new ReportParameter[repParams];

parameters[0] = new ReportParameter("p_Period", _period1.ToString());

parameters[1] = new ReportParameter("p_Period1", _period2.ToString());

if (repParams >= 3)

{

parameters[2] = new ReportParameter("p_DivisionID", _division.ToString());

}

if (repParams >= 4)

{

parameters[3] = new ReportParameter("p_DepartmentID", _department.ToString());

}

if (repParams == 5)

{

parameters[4] = new ReportParameter("p_EmployeeID", _employee.ToString());

}

this.ReportViewer1.ServerReport.SetParameters(parameters);

//Export to File

string mimeType, encoding, extension, deviceInfo;

string[] streamids;

Microsoft.Reporting.WebForms.Warning[] warnings;

string format = "PDF"; //Desired format goes here (PDF, Excel, or Image)

deviceInfo = "<DeviceInfo>" + "<SimplePageHeaders>True</SimplePageHeaders>" + "</DeviceInfo>";

byte[] bytes = ReportViewer1.ServerReport.Render(format, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);

Response.Clear();

if (format == "PDF")

{

Response.ContentType = "application/pdf";

Response.AddHeader("Content-disposition", "filename=" + _ReportName + ".pdf");

}

else if (format == "Excel")

{

Response.ContentType = "application/excel";

Response.AddHeader("Content-disposition", "filename=" + _ReportName + ".xls");

}

Response.OutputStream.Write(bytes, 0, bytes.Length);

Response.OutputStream.Flush();

Response.OutputStream.Close();

Response.Flush();

Response.Close();

//To View Report

//this.ReportViewer1.ServerReport.Refresh();

}

catch (Exception EX)

{

String _error = EX.Message;

lblMessage.Text = _error;

}

}

Comments

  1. Can we do this for .xlsx, .xlsb and CSV formats ?

    ReplyDelete
  2. Programmatically exporting data from SQL databases offers flexibility and automation. Best Single Strategy This approach enables developers to create custom solutions for data extraction, transformation, and loading (ETL) processes.

    ReplyDelete
  3. Elevate your online presence with powerful dedicated servers Chicago. Experience unmatched performance and reliability for your business needs.

    ReplyDelete

Post a Comment

Popular posts from this blog

Cloud Computing Technology Assessment

Database Testing With DBUnit

Data Science, Big Data & Microsoft Machine Learning