none
Excel file saving RRS feed

  • Question

  • Hi,

    I created excel file using the following code

    HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");
     HttpContext.Current.Response.Write("<Table border='0' width='100%'  " +
                  " cellSpacing='0' cellPadding='0' " +
                  "style='font-size:10.0pt; font-family:Calibri; background:white;' style='border-top:  solid black; border-left:  solid black; border-right:  solid black'> ");
                
                HttpContext.Current.Response.Write("<tr style='border-color: #cccccc; border-style:  solid; border-width: 1px;'>");
                HttpContext.Current.Response.Write("<td colspan='10' align='center'> Inspection Record");
                HttpContext.Current.Response.Write("</td>");
                HttpContext.Current.Response.Write("</tr>");
    
                HttpContext.Current.Response.Write("<tr style='border-color: #cccccc; border-style:  solid; border-width: 1px;'>");
                for (int i = 1; i <= 10; i++)
                {
                    HttpContext.Current.Response.Write("<td >");
                    HttpContext.Current.Response.Write("</td>");
                }
                HttpContext.Current.Response.Write("</tr>");
     HttpContext.Current.Response.Write("</table>");
                HttpContext.Current.Response.End
    using this code Open or Save option is coming. But I want to save the excel file directly to the file path. Can anyone help?
    • Moved by Kristin Xie Tuesday, September 23, 2014 8:01 AM
    Sunday, September 21, 2014 1:29 PM

All replies

  • Whatever the answer to your question, please remove the call to Response.End and replace it with:

    // Sends the response buffer
    Response.Flush()
    
    // Prevents any other content from being sent to the browser
    Response.SuppressContent = True
    
    // Directs the thread to finish, bypassing additional processing
    HttpContext.Current.ApplicationInstance.CompleteRequest()

    http://stackoverflow.com/a/11505401/736079


    My blog: blog.jessehouwing.nl

    Sunday, September 21, 2014 7:30 PM
  • Hi Kumar,

    This forum is to discuss problems of C# development. Your question is not related to the topic of this forum.

    Please post in the related ASP.Net Forum http://forums.asp.net
    for more efficient responses, where you can contact ASP.NET experts.

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 23, 2014 8:01 AM