I have a modal popup with a button control. When this button is clicked, I execute the following code which gets executed without any error but it won't ask to open or save the file. Also the excel file is not created, instead I see an diagnostic xml file having reference to this filename when I search for the file.
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application / vnd.ms - excel";
Response.AddHeader("content - disposition", "attachment; filename = MyFiles.xls");
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
dgCurrentPoBdetails.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
I've tried using the following code; it does create an Excel file but does not prompt me to open or save. Also, I am not able to open up this file; it give me an error "Excel cannot open this file because the file format of the file extension is invalid"
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
// Render grid view control.
dgCurrentPoBdetails.RenderControl(htw);
// Write the rendered content to a file.
string renderedGridView = sw.ToString();
System.IO.File.WriteAllText(@"C:\MoQ\ExportedFile.xlsx", renderedGridView);
string myStringVariable = "File saved to: " + "C:\\MyPath\\ExportedFile.xlsx";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert(" + myStringVariable + ")", true);