Hi jwilliamsdeve...,
Use closedXML library.
For more details refer below article.
HTML
<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="OnPageIndexChanging">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" ItemStyle-Width="150px" />
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:HyperLink ID="lnkCity" runat="server" NavigateUrl="#" Text='<%# Eval("City") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country1">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text = "CheckBox Control" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country2">
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" Text = "RadioButton Control" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick="ExportToExcel" />
Namespaces
using ClosedXML.Excel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
protected void ExportToExcel(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
this.BindGrid();
DataTable dt = new DataTable("GridView");
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dt.Columns.Add(cell.Text);
}
foreach (GridViewRow row in GridView1.Rows)
{
dt.Rows.Add();
int i = 0;
foreach (TableCell cell in row.Cells)
{
List<Control> controls = new List<Control>();
if (cell.Controls.Count == 0)
{
dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text;
}
foreach (Control control in cell.Controls)
{
controls.Add(control);
}
foreach (Control control in controls)
{
switch (control.GetType().Name)
{
case "HyperLink":
dt.Rows[dt.Rows.Count - 1][i] = (control as HyperLink).Text;
break;
case "TextBox":
dt.Rows[dt.Rows.Count - 1][i] = (control as TextBox).Text;
break;
case "LinkButton":
dt.Rows[dt.Rows.Count - 1][i] = (control as LinkButton).Text;
break;
case "CheckBox":
dt.Rows[dt.Rows.Count - 1][i] = (control as CheckBox).Text;
break;
case "RadioButton":
dt.Rows[dt.Rows.Count - 1][i] = (control as RadioButton).Text;
break;
}
cell.Controls.Remove(control);
}
i++;
}
}
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt);
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=GridView.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
Screenshot