2014-02-22 50 views
-3

這是我試圖將數據保存到excel中的代碼,但是在數據保存後,頁面凍結,無法重定向到其他頁面。將數據保存到excel後重新加載aspx頁面

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

這是通過對此我試着保存代碼數據轉換爲excel,但在數據保存後,頁面凍結,無法重定向到其他頁面。 – user3148561

回答

0

你只需要在save()函數的末尾調用Response.Redirect("currentpage url here");

在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 
} 
+0

已經做到了,無法實現任何目標。 – user3148561

+0

在所有'Response.Write()'語句後調用'Response.End()' –

+0

是的,但我停止了頁面的執行。 – user3148561

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>"); 

     } 
相關問題