Dear nauna,
Please refer below Sample.
HTML
<asp:GridView ID="gvItems" runat="server"></asp:GridView>
<br />
<asp:Button ID="btnConvert" runat="server" Text="Convert" OnClick="Convert" />
Namespaces
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Item"), new DataColumn("Price") });
dt.Rows.Add("Shirt", 500);
dt.Rows.Add("Jeans", 2000);
dt.Rows.Add("Trousers", 1545);
dt.Rows.Add("Tie", 200);
dt.Rows.Add("Cap", 300);
dt.Rows.Add("Hat", 350);
dt.Rows.Add("Scarf", 140);
dt.Rows.Add("Belt", 400);
ViewState["dt"] = dt;
BindGrid(dt, false);
}
}
private void BindGrid(DataTable dt, bool rotate)
{
gvItems.ShowHeader = !rotate;
gvItems.DataSource = dt;
gvItems.DataBind();
if (rotate)
{
dt.Columns.RemoveAt(0);
gvItems.DataSource = dt;
gvItems.DataBind();
foreach (GridViewRow row in gvItems.Rows)
{
row.Cells[0].CssClass = "header";
}
}
}
protected void Convert(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
DataRow dr = dt.NewRow();
dr[0] = "EmailId";
dt.Rows.InsertAt(dr, 0);
dr = dt.NewRow();
dr[0] = "UserName";
dt.Rows.InsertAt(dr, 1);
dr = dt.NewRow();
dr[0] = "Date";
dt.Rows.InsertAt(dr, 2);
DataTable dt2 = new DataTable();
for (int i = 0; i <= dt.Rows.Count; i++)
{
dt2.Columns.Add();
}
for (int i = 0; i < dt.Columns.Count; i++)
{
dt2.Rows.Add();
dt2.Rows[i][0] = dt.Columns[i].ColumnName;
}
for (int i = 0; i < dt.Columns.Count; i++)
{
for (int j = 0; j < dt.Rows.Count; j++)
{
dt2.Rows[i][j + 1] = dt.Rows[j][i];
}
}
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);
gvItems.AllowPaging = false;
BindGrid(dt2, true);
gvItems.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Item"), New DataColumn("Price")})
dt.Rows.Add("Shirt", 500)
dt.Rows.Add("Jeans", 2000)
dt.Rows.Add("Trousers", 1545)
dt.Rows.Add("Tie", 200)
dt.Rows.Add("Cap", 300)
dt.Rows.Add("Hat", 350)
dt.Rows.Add("Scarf", 140)
dt.Rows.Add("Belt", 400)
ViewState("dt") = dt
BindGrid(dt, False)
End If
End Sub
Private Sub BindGrid(ByVal dt As DataTable, ByVal rotate As Boolean)
gvItems.ShowHeader = Not rotate
gvItems.DataSource = dt
gvItems.DataBind()
If rotate Then
dt.Columns.RemoveAt(0)
gvItems.DataSource = dt
gvItems.DataBind()
For Each row As GridViewRow In gvItems.Rows
row.Cells(0).CssClass = "header"
Next
End If
End Sub
Protected Sub Convert(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = CType(ViewState("dt"), DataTable)
Dim dr As DataRow = dt.NewRow()
dr(0) = "EmailId"
dt.Rows.InsertAt(dr, 0)
dr = dt.NewRow()
dr(0) = "UserName"
dt.Rows.InsertAt(dr, 1)
dr = dt.NewRow()
dr(0) = "Date"
dt.Rows.InsertAt(dr, 2)
Dim dt2 As DataTable = New DataTable()
For i As Integer = 0 To dt.Rows.Count
dt2.Columns.Add()
Next
For i As Integer = 0 To dt.Columns.Count - 1
dt2.Rows.Add()
dt2.Rows(i)(0) = dt.Columns(i).ColumnName
Next
For i As Integer = 0 To dt.Columns.Count - 1
For j As Integer = 0 To dt.Rows.Count - 1
dt2.Rows(i)(j + 1) = dt.Rows(j)(i)
Next
Next
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Using sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
gvItems.AllowPaging = False
BindGrid(dt2, True)
gvItems.RenderControl(hw)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
End Using
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
Screenshot