SSRS (SQL Server Reporting Services) export to excel in console application in C#
Namespace:
using RestSharp;using RestSharp.Authenticators;
using System;
using System.IO;
private static void GenerateReport(string strReportPath,string _parameters, string FileName, string FileType = "Excel")
{
try
{
string strReportPath = strReportPath;//"UserDetails"
//string FileType = "Excel";
string URL = ConfigurationManager.AppSettings["ReportServerPath"].ToString() + "?" + strReportPath + "&rs:Command=Render&rs:Format=" + FileType + _parameters;
var client = new RestClient(URL);
client.Authenticator = new NtlmAuthenticator(ConfigurationManager.AppSettings["ReportUser"].ToString(), ConfigurationManager.AppSettings["ReportPwd"].ToString());
var request = new RestRequest(Method.GET);
Byte[] FileByte = client.DownloadData(request);
ByteToFile(FileName, FileByte, FileType);
}
catch (Exception ex)
{
throw;
}
}
static void ByteToFile(string FileName, Byte[] bytes, string FileType)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Data";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = AppDomain.CurrentDomain.BaseDirectory + "\\Data\\" + System.DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (FileType == "Excel")
{
FileName += ".xls";
}
else
{
FileName += ".pdf";
}
File.WriteAllBytes(path + "\\"+ FileName, bytes);
}
Excel Export Example:
GenerateReport("UserDetails", "&From=11-Aug-2020&UpTo=01-Jan-2021", "UserDetails","Excel');
PDF Export Example:
GenerateReport("UserDetails", "&From=11-Aug-2020&UpTo=01-Jan-2021", "UserDetails","Pdf');
No comments:
Post a Comment