Hi gibson,
I have created a sample which full fill your requirement you need to the code according to your requirement.
HTML
<div>
<asp:GridView ID="gvCustomers" runat="server" />
<br />
<asp:Button ID="btnExport" Text="ExportToExcel" runat="server" OnClick="btnExport_Click" />
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
for (int i = 0; i < gvCustomers.HeaderRow.Cells.Count - 1; i++)
{
dt.Columns.Add(gvCustomers.HeaderRow.Cells[i].Text.ToString());
}
foreach (GridViewRow row in gvCustomers.Rows)
{
DataRow dr = dt.NewRow();
dr["CustomerID"] = row.Cells[0].Text;
dr["CompanyName"] = row.Cells[1].Text;
dr["ContactName"] = row.Cells[2].Text;
dr["City"] = row.Cells[3].Text;
dr["Country"] = row.Cells[4].Text;
dt.Rows.Add(dr);
}
//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=Customers.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//Apply text style to each Row
GridView1.Rows[i].Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT TOP 10 CustomerID,CompanyName,ContactName,City,Country,PostalCode FROM Customers";
cmd.Connection = con;
con.Open();
gvCustomers.DataSource = cmd.ExecuteReader();
gvCustomers.DataBind();
con.Close();
}
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGrid()
End If
End Sub
Protected Sub btnExport_Click(sender As Object, e As EventArgs)
Dim dt As New DataTable()
For i As Integer = 0 To gvCustomers.HeaderRow.Cells.Count - 2
dt.Columns.Add(gvCustomers.HeaderRow.Cells(i).Text.ToString())
Next
For Each row As GridViewRow In gvCustomers.Rows
Dim dr As DataRow = dt.NewRow()
dr("CustomerID") = row.Cells(0).Text
dr("CompanyName") = row.Cells(1).Text
dr("ContactName") = row.Cells(2).Text
dr("City") = row.Cells(3).Text
dr("Country") = row.Cells(4).Text
dt.Rows.Add(dr)
Next
'Create a dummy GridView
Dim GridView1 As New GridView()
GridView1.AllowPaging = False
GridView1.DataSource = dt
GridView1.DataBind()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=Customers.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
For i As Integer = 0 To GridView1.Rows.Count - 1
'Apply text style to each Row
GridView1.Rows(i).Attributes.Add("class", "textmode")
Next
GridView1.RenderControl(hw)
'style to format numbers to string
Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.[End]()
End Sub
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand()
cmd.CommandText = "SELECT TOP 10 CustomerID,CompanyName,ContactName,City,Country,PostalCode FROM Customers"
cmd.Connection = con
con.Open()
gvCustomers.DataSource = cmd.ExecuteReader()
gvCustomers.DataBind()
con.Close()
End Using
End Using
End Sub
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub
ScreenShot
