C# .NET - Converting ".RDLC" Report File To Excel

Asked By MILAN SHAH on 22-Jan-09 06:33 AM
Hello Friends,

I have created one report in .NET reporting services with extension "<<Reportname>>.rdlc" then format change in excel.

Ex.

Suppose i have prepare report for Balance Sheet. in that in Credit column there is a group total, i have differentiate that group total by dotted line. when i am exporting that report to excel document then the whole column's data type is going to be string.
so any solution to remain decimal field in excel.


re - Web Star replied to MILAN SHAH on 22-Jan-09 06:53 AM

use this

private void m_ibtExportExl_Click(object sender, ImageClickEventArgs e)

{

formatRender="EXCEL";

Byte[] response=null;

System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

string dirPath=AppDomain.CurrentDomain.BaseDirectory + "Uploads/Reports";

string fPath="";

try

{

if(ViewState["strCondition"].ToString() !="")

{

RegisterClientScriptBlock("RM","<script>displayProgressBar();</script>");

objRptAPI=new MEDNET.BLLReportModule.BLLPayerTransaction();

objRptAPI.Condition=ViewState["strCondition"].ToString();

objRptAPI.ConnectionString=((SessionData)Session["UserSessionData"]).DatabaseCredentials;

objRptAPI.SortExpr= ddlSortBy.SelectedValue.Trim();

objRptAPI.Format=formatRender;

objRptAPI.GroupBy=ddlGroupBy.SelectedValue.Trim();

objRptAPI.ClientTime = newdate;

response = objRptAPI.RenderReport();  //method in that class

if (Directory.Exists(dirPath))

{

fPath=dirPath + "/Report" + reportName + ".xls";

}

else

{

Directory.CreateDirectory(dirPath);

fPath=dirPath + "/Report" + reportName + ".xls";

}

if(File.Exists(fPath))

File.Delete(fPath);

FileStream fStream=File.OpenWrite(fPath);

fStream.Write(response,0,response.Length);

fStream.Close();

DownLoadFile(fPath,true);

Response.ClearContent();

Response.ClearHeaders();

Response.ContentType="application/msexcel";

}

}

catch(Exception ex)

{

strErr=ex.Message;

}

}

and in class method is as followes

public Byte[] RenderReport()

{

Byte[] response=null;

try

{

System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

string username=ConfigurationSettings.AppSettings["username"].ToString();

string password=ConfigurationSettings.AppSettings["password"].ToString();

string domain=ConfigurationSettings.AppSettings["domain"].ToString();

objExec.Credentials= new NetworkCredential(username,password,domain);

BLLReportModule.ReportExecution.ReportParameter rptParams = new BLLReportModule.ReportExecution.ReportParameter();

ExecutionHeader header=new ExecutionHeader();

rptPath="/Report Project/rptFinancialClassTransaction";

ExecutionInfo rptInfo = objExec.LoadReport(rptPath, null);

objExec.ExecutionHeaderValue = header;

objExec.ExecutionHeaderValue.ExecutionID = rptInfo.ExecutionID;

string Format =this.Format;

string DeviceInfo = "<DeviceInfo><HTMLFragment>True</HTMLFragment>";

DeviceInfo += "<Parameters>False</Parameters><Toolbar>True</Toolbar></DeviceInfo>";

string Encoding = null;

string MimeType = null;

string Extension=null;

BLLReportModule.ReportExecution.Warning[] Warnings = null;

string[] StreamIDs = null;

BLLReportModule.ReportExecution.ParameterValue[] rptParameters = new BLLReportModule.ReportExecution.ParameterValue[6];

string strCon=this.ConnectionString;

string[] arr=strCon.Split(";".ToCharArray());

string []fStr=null;

string server="",database="";

for(int i=0;i<=arr.Length-3;i++)

{

fStr=arr[i].Split("=".ToCharArray());

if(i==0)

server=fStr[1].ToString();

else

database=fStr[1].ToString();

}

rptParameters.SetValue(GetParameters(server,"ServerName"),0);

rptParameters.SetValue(GetParameters(database,"DBName"),1);

rptParameters.SetValue(GetParameters(Convert.ToString(this.Condition),"search"),2);

rptParameters.SetValue(GetParameters(Convert.ToString(this.SortExpr),"SORTBY"),3);

rptParameters.SetValue(GetParameters(Convert.ToString(this.GroupBy),"Groupby"),4);

rptParameters.SetValue(GetParameters(Convert.ToString(this.ClientTime),"ClientTime"),5);

objExec.SetExecutionParameters(rptParameters,"en-us");

response = objExec.Render(Format,DeviceInfo,out Extension, out MimeType,out Encoding,out Warnings, out StreamIDs);

}

catch(Exception ex)

{

string strErr=ex.Message + ex.Source + ex.StackTrace + ex.TargetSite.Name ;

}

return response;

}