Export Grid View to Excel

G

Guest

I have a Grid View and I want to expoet into Excel. I am using a hyperlink
to export the GridView intop Excel.

But when I click on link it just shows me java script. It doesn't open
Excel sheet. Here is my Code.

In HTML

<p><asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="javascript:alert('Unable to process request');">Send to
Excel</asp:HyperLink></p>

In Code behind file

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{


if (!string.IsNullOrEmpty(Request.QueryString["m"]) &&
Request.QueryString["m"] == "xls")
{
isExportToExcel = true;
sendResultsToExcel();
}
}

}

protected void sendResultsToExcel()
{
// setup Grid View
gdExceptions.AllowPaging = false;
gdExceptions.ShowFooter = false;
gdExceptions.ShowResultSummary = false;
gdExceptions.Columns[3].Visible = false; // hide link

gdExceptions.DataBind();


// export to excel
Response.Clear();
Response.Buffer = true;

Response.AddHeader("content-disposition",
"attachment;filename=P3Exceptions.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";

this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(gdExceptions);

oHtmlTextWriter.WriteLine("<style>.text { mso-number-format:\\@; }
</style>");
gdExceptions.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top