Display gridview result in an excel

Posted by Bhadri under C# on 1/9/2013 | Points: 10 | Views : 1088 | Status : [Member] | Replies : 2
In my code behind , I have converted the datatable into a list of Entities to display in the view. I am binding this list to my gridview. I am able to get the data in the grid view.
Now, I want to view this data in an excel instead of a grid view i.e. my result should appear in excel instead of displaying in the gridview.
It is not an export to Excel. I want my result to be populated in the excel sheet instead of getting displayed in the gridview.
Please help me to achieve this.


Posted by: Nisar87 on: 1/18/2013 [Member] Starter | Points: 25

Why are you thinking differently while displaying records in excel. Just create a gridview at runtime and assign the datatable to that grid. Now grid is populated but will not show to the users because grid is not added any control.
Now simply export that grid to excel. and you will achieve what you are looking for.
I hope you get my point.
GridView Grd = Fill_Grid_Func();
GridViewExportUtil.Export("Customers.xls", Grd);
Grd = null;

for Export please refer following link

Nisar Ahmad

Bhadri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/21/2013 [Member] [MVP] Silver | Points: 25

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="import_the_gridview_data_in_excel._Default" %>

<!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>Untitled Page</title>
<form id="form1" runat="server">

<asp:GridView ID="GridView1" runat="server">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Import to Excel" />

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
namespace import_the_gridview_data_in_excel
public partial class _Default : System.Web.UI.Page
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter sqlda = new SqlDataAdapter("SELECT * from employee", conn);
DataSet ds = new DataSet();
sqlda.Fill(ds, "employee");
GridView1.DataSource = ds;
GridView1.DataMember = "employee";

protected void Button1_Click(object sender, EventArgs e)
ExportToExcel("Report.xls", GridView1);
private void ExportToExcel(string strFileName, GridView dg)
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

public override void VerifyRenderingInServerForm(Control control)

If this post helps you mark it as answer

Bhadri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response