将数据保存到Excel后重新加载aspx页面

时间:2014-02-22 09:26:24

标签: c# asp.net excel

这是我尝试将数据保存到Excel中的代码,但在保存数据后,页面冻结,无法重定向到其他页面。

 public void Save(Literal L) 
  { 
      Response.ContentType =

      "application/ms-excel";
          Response.AddHeader("content-disposition", "attachment; filename=Report.xls");
          Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
          Response.Write("<head>");
          Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html;  charset=windows-1252\">");
          Response.Write("<!--[if gte mso 9]>");
          Response.Write("<xml>");
          Response.Write("<x:ExcelWorkbook>");
          Response.Write("<x:ExcelWorksheets>");
          Response.Write("<x:ExcelWorksheet>");
          //this line names the worksheet
          Response.Write("<x:Name>Report</x:Name>");
          Response.Write("<x:WorksheetOptions>");
          //these 2 lines are what works the magic
          Response.Write("<x:Panes>");
          Response.Write("</x:Panes>");
          Response.Write("</x:WorksheetOptions>");
          Response.Write("</x:ExcelWorksheet>");
          Response.Write("</x:ExcelWorksheets>");
          Response.Write("</x:ExcelWorkbook>");
          Response.Write("</xml>");
          Response.Write("<![endif]-->");
          Response.Write("</head>");
          Response.Write("<body>");
          Response.Write(L.Text);
          Response.Write("</body>");
          Response.Write("</html>"); }

2 个答案:

答案 0 :(得分:0)

您只需在Response.Redirect("currentpage url here");函数的末尾调用save()

在save方法的所有语句的末尾调用Response.End()。

public void Save(Literal L) 
{ 
  Response.ContentType = "application/ms-excel";
  Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
  Response.Write("<head>");
  ------------------
  --------------------

 Response.End();//call this
}

答案 1 :(得分:0)

添加以下代码行

          public void Save(Literal L) 
  { 
      Response.ContentType =

      "application/ms-excel";
          Response.AddHeader("content-disposition", "attachment; filename=Report.xls");
          Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
          Response.Write("<head>");
          Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html;  charset=windows-1252\">");
          Response.Write("<!--[if gte mso 9]>");
          Response.Write("<xml>");
          Response.Write("<x:ExcelWorkbook>");
          Response.Write("<x:ExcelWorksheets>");
          Response.Write("<x:ExcelWorksheet>");
          //this line names the worksheet
          Response.Write("<x:Name>Report</x:Name>");
          Response.Write("<x:WorksheetOptions>");
          //these 2 lines are what works the magic
          Response.Write("<x:Panes>");
          Response.Write("</x:Panes>");
          Response.Write("</x:WorksheetOptions>");
          Response.Write("</x:ExcelWorksheet>");
          Response.Write("</x:ExcelWorksheets>");
          Response.Write("</x:ExcelWorkbook>");
          Response.Write("</xml>");
          Response.Write("<![endif]-->");
          Response.Write("</head>");
          Response.Write("<body>");
          Response.Write(L.Text);
          Response.Write("</body>");
          Response.Write("</html>");
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Data has been saved');window.location.replace('pagename.aspx');</script>");

         }