Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Export Gridview to Excel help....

$
0
0

I have an C# ASPX page with a Gridview and aButton.  I am trying to export the Gridview to MS Excel and I am not getting an error from my codes below but I'm not getting an Excel File.

How do I fix my code to launch the MS Excel File....?


Code

try
   {
    Label1.Text = "You clicked the link button";  <---This is a test -- button works fine.
    //Export the GridView to Excel ===
                    
    Response.Clear();
    Response.Buffer = true;
    //***************************************************************************                               
    string attachment = "attachment; filename={0} company_name" + DateTime.Now.Date + ".xls";

    Response.AddHeader("content-disposition", attachment);
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    //************** MS Excel Header *********************************************
    sw.WriteLine("<b><u><font size='3'>company name page title</font></u></b>");
    sw.WriteLine("<br />");

    //****************************************************************************
    myGridView.AllowPaging = false;
    myGridView.RenderControl(hw);

    string style = "<style>.textmode{mso-number-format:\\@;}</style>";
    Response.Write(style);

    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
    //************************************************************************
  }
  catch (Exception ex)
  {
    lblErrorMsg.Text = ex.Message;
    PNError.Visible = true;
  }


One error message:
[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

What does this mean and how do I fix this error?


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>