Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to export data from gridview to excel but the spreadsheet comes up empty
Here is part of my code below.
C#
protected void btnExport_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=Report.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.xls";
            StringWriter StringWriter = new System.IO.StringWriter();
            HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
            GridView1.RenderControl(HtmlTextWriter);
            
            Response.Write(StringWriter.ToString());
            Response.End();
        }
       
        public override void VerifyRenderingInServerForm(Control control)
        {

        }
Posted
Updated 20-Aug-12 6:30am
v2
Comments
Withation 29-Mar-16 7:52am    

I have suggested an alternate way in this article.

Check that.
Export DataTable to Excel with Formatting in C#
 
Share this answer
 
v3
Comments
mantel1 20-Aug-12 11:22am    
Thanks Santhosh. I'm actually reading the data from a sql database. Any suggestions on that approach?

Thanks
Santhosh Kumar Jayaraman 20-Aug-12 11:24am    
Ya you can retrieve records from sql database and store it in datatable. Then try my method.
protected void Button1_Click(object sender, EventArgs e)

{

    Response.AddHeader("content-disposition", 
       "attachment;filename=FileName.xls");

    Response.Charset = String.Empty;

    Response.ContentType = "application/vnd.xls";

    System.IO.StringWriter sw = new System.IO.StringWriter();

    System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.RenderControl(hw);

    Response.Write(sw.ToString());

    Response.End();

}
 
Share this answer
 
Comments
AmitGajjar 21-Aug-12 0:29am    
why you have posted two solution, instead you can improve solution in single one.
Here is the complete code to Export GridView to Excel:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; 

public partial class ExportGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridView1.DataSource = BindData();
            GridView1.DataBind(); 
        }
    }

    

    private string ConnectionString
    {

        get { return @"Server=localhost;Database=Northwind;
        Trusted_Connection=true"; }

    }

    

    private DataSet BindData()
    {
        // make the query 
        string query = "SELECT * FROM Categories";
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
        DataSet ds = new DataSet();
        ad.Fill(ds, "Categories");
        return ds;

    }




    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();

        Response.AddHeader("content-disposition", "attachment;
        filename=FileName.xls");

        Response.Charset = "";

        // If you want the option to open the Excel file without saving than

        // comment out the line below

        // Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.ContentType = "application/vnd.xls";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter htmlWrite =
        new HtmlTextWriter(stringWrite);

        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());

        Response.End();

    }

    public override void VerifyRenderingInServerForm(Control control)
    {

        // Confirms that an HtmlForm control is rendered for the
        specified ASP.NET server control at run time.

    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900