Export Gridview Data to Excel in ASP.NET with C#
Export GridView to Excel in ASP.Net with Formatting:
In this Post we try to transfer grid view data to excel by
C# code for this we give a very sort and efficient Example of export your data
which are display in gridview and datagrid control are exported into excel format
in asp.net application.
Examples related to GridView:
- How to Bind Gridview Form database.
- Export GridView data to txt file in Asp.net using C#
- Show gridview Row Details.
- Templatefield in gridview
- Introduction of Asp.net grid view Control.
- Example of Templatefield in gridview .
- Example of DropDownList inside GridView
- Check box in ASP.NET GridView
- Ckeck box list example using javascript in grid.
- Check box in ASP.NET GridView
- Show grid view row details in to tooltip.
- How to Bind Gridview Form database.
- Show gridview Row Details And Give Example.
- Example of Templatefield in asp.net gridview.
- Example of DropDownList inside GridView control
- Introduction of Asp.net grid view Control.
- Example of C# for Bind Data to asp.net Textbox inside gridview control
Create DataTable for Export gridview to excel:
Now before creating the application, let us create a DataTable
with some records for the GridView, the DataTable has the following fields:
- Product Id,
- Product Name,
- Price,
- Quantity.
Create DataTable by C# code:
DataTable dt = new
DataTable();
dt.Columns.Add("ProductId",
typeof(Int32));
dt.Columns.Add("ProductName",
typeof(string));
dt.Columns.Add("Price", typeof(string));
dt.Columns.Add("Quantity", typeof(string));
dt.Rows.Add(1, "Lux", "10Rs","10");
dt.Rows.Add(2, "Mobile", "10000Rs","20");
dt.Rows.Add(3, "Sugar", "50/kg","10");
dt.Rows.Add(4, "Biskit", "20Rs","10");
Change the Header Row back to white color:
gvDetails.HeaderRow.Style.Add("background-color",
"#FFFFFF");
Export GridView data to excel using ASP.NET and c#:
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="ExportGridviewData.aspx.cs"
Inherits="ExportGridviewData"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Export Gridview Data in to Excel by C#</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:GridView ID="gvDetails"
runat="server"
AutoGenerateColumns="false"
Height="186px">
<Columns>
<asp:BoundField DataField="ProductId"
HeaderText="Product
Id" ItemStyle-Width="100">
<ItemStyle
Width="100px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="ProductName"
HeaderText="Product
Name" ItemStyle-Width="150">
<ItemStyle
Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Price"
HeaderText="Price"
ItemStyle-Width="150">
<ItemStyle
Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Quantity"
HeaderText="Quantity"
ItemStyle-Width="150">
<ItemStyle
Width="150px"></ItemStyle>
</asp:BoundField>
<%--<asp:BoundField DataField="Quantity"
HeaderText="Quantity" ItemStyle-Width="150">
<ItemStyle
Width="150px"></ItemStyle>
</asp:BoundField>--%>
</Columns>
<HeaderStyle BackColor="#990000"
ForeColor="White"
BorderColor="#0000CC"
BorderStyle="Groove"
/>
</asp:GridView>
<br />
<asp:Button ID="Button1"
runat="server"
Text="Export to
Excel" OnClick="btnExport_Click" />
</div>
</form>
</body>
</html>
C# code for convert grid data in t Excel File:
using System;
using System.Data;
using System.IO;
using System.Web.UI;
public partial class ExportGridviewData
: System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
protected void
BindGridview()
{
DataTable dt = new
DataTable();
dt.Columns.Add("ProductId",
typeof(Int32));
dt.Columns.Add("ProductName",
typeof(string));
dt.Columns.Add("Price", typeof(string));
dt.Columns.Add("Quantity", typeof(string));
dt.Rows.Add(1, "Lux", "10Rs","10");
dt.Rows.Add(2, "Mobile", "10000Rs","20");
dt.Rows.Add(3, "Sugar", "50/kg","10");
dt.Rows.Add(4, "Biskit", "20Rs","10");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void
btnExport_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition",
string.Format("attachment;
filename={0}", "ProductList.xls"));
Response.ContentType = "application/ms-excel";
StringWriter
sw = new StringWriter();
HtmlTextWriter htw = new
HtmlTextWriter(sw);
gvDetails.AllowPaging = false;
BindGridview();
//Change the Header Row back to white color
gvDetails.HeaderRow.Style.Add("background-color",
"#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i
< gvDetails.HeaderRow.Cells.Count; i++)
{
gvDetails.HeaderRow.Cells[i].Style.Add("background-color",
"#990000");
}
gvDetails.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
}
Export Gridview Data to Excel |
Asp.net related Post :
- Asp.net Email Address Validation Using Regular Expression
- Find Nth Highest Salary in SQL Server
- How to Create Sql Function, use of function in sql
- SQL Injection,how we prevent over asp.net web application
- Sql CONTINUE STATEMENT,how to use
- Sql server while loop statement how to use
- SQL Features and Benefits, Database Engine
- Find datetime difference in asp.net by C#
- In Asp.net Difference between ""(empty string) and String.Empty
- Asp.net NullReferenceException and how fix it
- Add rows in GridView dynamic with Textbox
- Add rows in GridView dynamic with Textbox
- In asp.net by jquery change div text color on mouseover
- Asp.net Watermark Text on uploaded Image
- Asp.net TextBox OnTextChanged Event
- Upload multiple by one asp.net fileupload control using jQuery
- Create Dynamic Rows in ASP.Net GridView Control with TextBoxes
- Limitation of Characters in Textbox or TextArea in asp.netusing jquery:
- jquery disable or Enable submit button after validation
- Enable Disable Submit Button using jQuery
- Get the current page url by C# jQuery modal dialog with postbacks
- Drag and Drop Sortable Lists using jQueryUI
- jquery tooltip with css in asp.net web page
- Ajax ColorPickerExtender in ASP.NET Example
- What is the Ajax colorpicker,How to use Ajax colorpicker
- JQuery UI Datepicker (Calendar) with asp.net textbox
Comments
Post a Comment