exporting gridview to excel

Discussion in 'ASP .Net' started by Mike P, Jun 8, 2006.

  1. Mike P

    Mike P Guest

    I am using the following code to export my gridview to excel :

    protected void btnExport_Click(object sender, ImageClickEventArgs 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.ms-excel";

    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.
    }

    My problem is that I am setting the text values of one of the columns in
    the RowDataBound event, and doing so seems to mean that the text for
    this column is not exported, whereas if I don't set the text in the
    RowDataBound event, the text is exported.

    Here is my code for RowDataBound:

    switch (Probability.Text)
    {
    case "0.25":
    e.Row.Cells[4].Text = "A (25%)";
    e.Row.Cells[4].BackColor =
    System.Drawing.Color.PowderBlue;
    break;

    case "0.5":
    e.Row.Cells[4].Text = "B (50%)";
    e.Row.Cells[4].BackColor =
    System.Drawing.Color.DeepSkyBlue;
    break;

    Can anybody help with this?

    *** Sent via Developersdex http://www.developersdex.com ***
    Mike P, Jun 8, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia
    Replies:
    1
    Views:
    1,371
    Carl Prothman [MVP]
    Jan 12, 2005
  2. Replies:
    3
    Views:
    7,677
    reena_pgdi
    May 21, 2010
  3. Tom
    Replies:
    4
    Views:
    450
    Steven Cheng[MSFT]
    Dec 19, 2006
  4. =?Utf-8?B?Vmlua2k=?=

    lost footer values when exporting gridview to excel

    =?Utf-8?B?Vmlua2k=?=, Jul 24, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    1,358
    Edward Wood
    Jan 31, 2011
  5. Doogie
    Replies:
    1
    Views:
    443
    sloan
    Nov 19, 2008
Loading...

Share This Page