Pages

Tuesday, March 12, 2013

Export SSRS to Excel using C# code.

<asp:LinkButton ID="linkProcess" runat="server" OnClick="linkProcess_Click"><b>Export to Excel</b></asp:LinkButton>

/// <summary>
/// method to export report into excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void linkProcess_Click(object sender, EventArgs e)
{
Byte[] returnValue;
returnValue = rvManagementReports.ServerReport.Render("EXCEL");
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment ; filename=filename." + "xls");
Response.BinaryWrite(returnValue);
Response.Flush();
Response.End();
}





for any language character use

/// <summary>
/// method to export report into excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void linkProcess_Click(object sender, EventArgs e)

{

Response.ClearContent();
 
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "YTDReviewsData"));

Response.ContentType = "application/ms-excel";

Response.ContentEncoding = System.Text.Encoding.Unicode;

Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);



gvDealDetails.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

}
 
 

Monday, March 11, 2013

ASP.net Gridview sorting

private string ConvertSortDirectionToSql(String sortExpression,SortDirection sortDireciton)
{
        switch (sortExpression)
{
case "DealCRMID":
ViewState["DealCRMID"]=ChangeSortDirection(ViewState["DealCRMID"].ToString());
return ViewState["DealCRMID"].ToString();
case "DealCustomerName":
ViewState["DealCustomerName"] = ChangeSortDirection(ViewState["DealCustomerName"].ToString());
return ViewState["DealCustomerName"].ToString();
case "SLCode":
ViewState["SLCode"] = ChangeSortDirection(ViewState["SLCode"].ToString());
return ViewState["SLCode"].ToString();
default:
return "ASC";
}
}
private string ChangeSortDirection(string sortDireciton)
{ switch (sortDireciton)
{ case "DESC":
return "ASC";
case "ASC":
return "DESC";
default:
return "ASC";

}
}



protected void gvPendingApprovals_Sorting(object sender, GridViewSortEventArgs e)
{
DataSet ds = (System.Data.DataSet)(gvPendingApprovals.DataSource);
if(ds.Tables.Count>0)
{
DataView m_DataView = new DataView(ds.Tables[0]);

m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortExpression.ToString(), e.SortDirection);

gvPendingApprovals.DataSource = m_DataView;

gvPendingApprovals.DataBind();

}

}



Markup:

<asp:BoundField DataField="DealCRMID" HeaderText="Opportunity ID" SortExpression="DealCRMID"/>

<asp:BoundField DataField="DealCustomerName" HeaderText="Customer" SortExpression="DealCustomerName"/>

<asp:BoundField DataField="SLCode" HeaderText="Practice" SortExpression="SLCode"/>