Hi Friends,
Here I will explain how to export gridview data to Excel using asp.net in c#.I have one gridview that has filled with user details now I need to export gridview data toexcel . To implement this functionality first we need to design aspx page like this
ON ASPX PAGE :-
<div>
<asp:ImageButton ID="imgbtnExport" runat="server" BorderColor="White" ToolTip="Export Data" ImageUrl="~/Excel-icon.png"
OnClick="imgbtnExport_Click" />
<table>
<tr>
<td colspan="2">
<asp:GridView ID="gvRecords" AutoGenerateColumns="False" runat="server" >
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" HeaderStyle-BackColor="Orange" />
<asp:BoundField DataField="fullname" HeaderText="Full Name" HeaderStyle-BackColor="Orange" />
<asp:BoundField DataField="DOB" HeaderText="DOB" HeaderStyle-BackColor="Orange"/>
<asp:BoundField DataField="Gender" HeaderText="Gender" HeaderStyle-BackColor="Orange"/>
<asp:BoundField DataField="MobileNo" HeaderText="Mobile No." HeaderStyle-BackColor="Orange"/>
<asp:BoundField DataField="Salary" HeaderText="Salary" HeaderStyle-BackColor="Orange"/>
<asp:BoundField DataField="Isactive" HeaderText="Isactive" HeaderStyle-BackColor="Orange"/>
<asp:TemplateField HeaderText="Remove" HeaderStyle-BackColor="Orange">
<ItemTemplate>
<asp:LinkButton ID="Label2" runat="server" >Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
ON CODE BEHIND ASPX.CS :-
First of all take your grid view data in viewstate like below..
public void fillDataGrid()
{
DataSet ds = new DataSet();
ds = manager.GetRecords();
gvRecords.DataSource = ds.Tables[0];
ViewState["Export"] = ds.Tables[0];
gvRecords.DataBind();
}
protected void imgbtnExport_Click(object sender, ImageClickEventArgs e) // Image Button To Export Grid Recod
{
string date = DateTime.Now.ToString("MM-dd-yyyy");
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=ExcelData.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvRecords.AllowPaging = false;
DataTable ds = ViewState["Export"] as DataTable;
DataGrid dg = new DataGrid();
dg.HeaderStyle.ForeColor = System.Drawing.Color.Blue;
dg.DataSource = ds;
dg.DataBind();
dg.RenderControl(hw);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
Now you can export your grid view data on excel sheet.
Save it and then open.
Here I will explain how to export gridview data to Excel using asp.net in c#.I have one gridview that has filled with user details now I need to export gridview data toexcel . To implement this functionality first we need to design aspx page like this
ON ASPX PAGE :-
<div>
<asp:ImageButton ID="imgbtnExport" runat="server" BorderColor="White" ToolTip="Export Data" ImageUrl="~/Excel-icon.png"
OnClick="imgbtnExport_Click" />
<table>
<tr>
<td colspan="2">
<asp:GridView ID="gvRecords" AutoGenerateColumns="False" runat="server" >
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" HeaderStyle-BackColor="Orange" />
<asp:BoundField DataField="fullname" HeaderText="Full Name" HeaderStyle-BackColor="Orange" />
<asp:BoundField DataField="DOB" HeaderText="DOB" HeaderStyle-BackColor="Orange"/>
<asp:BoundField DataField="Gender" HeaderText="Gender" HeaderStyle-BackColor="Orange"/>
<asp:BoundField DataField="MobileNo" HeaderText="Mobile No." HeaderStyle-BackColor="Orange"/>
<asp:BoundField DataField="Salary" HeaderText="Salary" HeaderStyle-BackColor="Orange"/>
<asp:BoundField DataField="Isactive" HeaderText="Isactive" HeaderStyle-BackColor="Orange"/>
<asp:TemplateField HeaderText="Remove" HeaderStyle-BackColor="Orange">
<ItemTemplate>
<asp:LinkButton ID="Label2" runat="server" >Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
ON CODE BEHIND ASPX.CS :-
First of all take your grid view data in viewstate like below..
public void fillDataGrid()
{
DataSet ds = new DataSet();
ds = manager.GetRecords();
gvRecords.DataSource = ds.Tables[0];
ViewState["Export"] = ds.Tables[0];
gvRecords.DataBind();
}
protected void imgbtnExport_Click(object sender, ImageClickEventArgs e) // Image Button To Export Grid Recod
{
string date = DateTime.Now.ToString("MM-dd-yyyy");
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=ExcelData.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvRecords.AllowPaging = false;
DataTable ds = ViewState["Export"] as DataTable;
DataGrid dg = new DataGrid();
dg.HeaderStyle.ForeColor = System.Drawing.Color.Blue;
dg.DataSource = ds;
dg.DataBind();
dg.RenderControl(hw);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
Now you can export your grid view data on excel sheet.
No comments:
Post a Comment