Hello Sir,
i have below code that i am using to export the Gridview data to excel sheet:
<%@ Page Language="C#" MasterPageFile="~/admin/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="admin_Default" Title="Untitled Page" EnableEventValidation="false"%>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<%--<form id="form1" runat="server">--%>
<cc2:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" > </cc2:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div style="padding-left:20px;line-height:1.5em;";>
<div style="width:100%";>
<div style="float:right;margin-right:250px";>
<asp:Button ID="Button1" runat="server" Text="Print Current Page" OnClick = "PrintCurrentPage" />
<asp:ImageButton ID="btn_export_list_to_excel" runat="server"
ImageUrl="../images/excel.jpg" AlternateText="Export List to Excel"
onclick="btn_export_list_to_excel_Click"/>
</div>
</div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" AllowPaging ="true"
DataKeyNames = "id" >
<Columns>
<asp:BoundField ItemStyle-Width = "150px" DataField = "id"
HeaderText = "CustomerID" />
<asp:BoundField ItemStyle-Width = "150px" DataField = "name"
HeaderText = "Name"/>
<asp:BoundField ItemStyle-Width = "150px" DataField = "school"
HeaderText = "School"/>
</Columns>
</asp:GridView>
<br />
<br />
<asp:Label ID="lbl_msg" runat="server" ForeColor="#0475BB" style="color: #0475BB;
font-family: Arial;line-height: 27px;margin-left: 240px;margin-top: 90px;position: absolute;text-align: left;"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<%--</form>--%>
</asp:Content>
Code behind code:
protected void btn_export_list_to_excel_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
////this.BindGrid();
this.grdview_search();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
But it is not working, please suggest the solution.
Regards
Rohit Sood