Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a excel file from GridView data and prompt user to download or save file.i know well how to do it using response.addheader... in asp.net.but if i do this way then there is a white background in the excel file and no row and column border shown but shown border of gridview data only.so i am thinking to work with office object library to create excel file and download with version independently like any excel version will work....codeproject experts kindly help me.....
Posted

Hi,

I'm using Matt Berseth solution. His web page is not available anymore so I'll c/p code here...
Helper class
C#
using System;
using System.Data;
using System.Configuration;
using System.IO;
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;

/// <summary>
/// GridView Export Utilities
/// </summary>
public class GridViewExportUtil
{
    /// <summary>
    /// Exports GridView to excel
    /// </summary>
    /// <param name="fileName">File name</param>
    /// <param name="gv">GridView to export</param>
    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);

                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

    /// <summary>
    /// Replace any of the contained controls with literals
    /// </summary>
    /// <param name="control"></param>
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                GridViewExportUtil.PrepareControlForExport(current);
            }
        }
    }
}

Make ASP.NET page with grid and call export on Button click
C#
GridViewExportUtil.Export("Customers.xls", this.gvCustomers);
 
Share this answer
 
Comments
mridul samadder 2-Feb-12 8:11am    
dear martin
plz read my q. again.ur code shows white background in excel which i did not expect.i wanted data be in cell and all cells border color will remain same as a normal excel file
 
Share this answer
 
Comments
mridul samadder 2-Feb-12 9:08am    
dear rajesh
i tested locally referencing dlls from netoffice.look like works well.will the users of my site be able to create and download excel file in their computers on what version they have? i think netoffice supports office version up to 2010 what if there is a latest version of office comes and user have that version instead previou version?
mridul samadder 2-Feb-12 9:29am    
also if i want to use my asp.net app in a online server then i think i need to have office installed in the server.how i goes please answer.i need to run it on web server....

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