OP has commented that the example in the link from Solution 1 doesn't work. I have confirmed this. Excel loads but the data from the grid is not exported.
I can't be bothered to work out why that version is not working so here is an alternative
Export GridView to Excel in ASP.Net with Formatting using C# and VB.Net[
^]
(I've verified that this code works, but I had to manually add the
OnPageIndexChanging
event - not sure why)
Here is my full code that worked:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
</head>
<body>
<form runat="server" id="form1">
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" ItemStyle-Width="150px" />
<asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="100px" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100px" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick = "Button1_Click" />
</form>
</body>
</html>
using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication4
{
public partial class Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void ExportGridToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (var sw = new StringWriter())
{
var hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
BindGrid();
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)
{
cell.BackColor = row.RowIndex % 2 == 0 ? GridView1.AlternatingRowStyle.BackColor : GridView1.RowStyle.BackColor;
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
const string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
private void BindGrid()
{
var constr = ConfigurationManager.ConnectionStrings["ConnectToDB"].ConnectionString;
using (var con = new SqlConnection(constr))
{
using (var cmd = new SqlCommand("SELECT CustomerId, ContactName, City, Country FROM Customers"))
{
using (var sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (var dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ExportGridToExcel();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
}
}