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();
}
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();
}