2013-03-06 45 views
0

我有一些代碼創建一個Excel工作表,然後提示用戶將它保存在某個地方,並將它附加到一個電子郵件併發送到某個地方。這一切工作正常,當我有Response.end()但我想刪除這個爲了重定向到一個新的頁面。當我刪除它並用response.redirect替換它時,它不會提示用戶保存它。任何想法,爲什麼這不是促使拯救了?response.redirect和response.end

代碼來創建/保存Excel表

Protected Sub ExportToExcel(sender As Object, e As EventArgs, ByVal strPath As String) 
    Response.ClearContent() 

    Response.AddHeader("content-disposition", "attachment; filename=GridViewToExcel.xls") 

    Response.ContentType = "application/excel" 

    Dim sWriter As New StringWriter() 

    Dim hTextWriter As New HtmlTextWriter(sWriter) 

    Dim hForm As New HtmlForm() 

    Panel1.Parent.Controls.Add(hForm) 

    hForm.Attributes("runat") = "server" 

    hForm.Controls.Add(Panel1) 

    hForm.RenderControl(hTextWriter) 

    ' Write below code to add cell border to empty cells in Excel file 
    ' If we don't add this line then empty cells will be shown as blank white space 

    Dim sBuilder As New StringBuilder() 

    sBuilder.Append("<html xmlns:v=""urn:schemas-microsoft-com:vml"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:x=""urn:schemas-microsoft-com:office:excel"" xmlns=""http://www.w3.org/TR/REC-html40""> <head><meta http-equiv=""Content-Type"" content=""text/html;charset=windows-1252""><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>ExportToExcel</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>") 
    sBuilder.Append(Convert.ToString(sWriter) & "</body></html>") 

    Response.Write(sBuilder.ToString()) 

    Dim fStream As FileStream = File.Create(strPath) 
    fStream.Close() 
    Dim Writer As New StreamWriter(strPath) 
    Writer.Write(sBuilder) 
    Writer.Flush() 
    Writer.Close() 
End Sub 

運行重定向

Response.Redirect("~/Default.aspx") 
+1

您無法向一個HTTP請求發送兩個響應。 – SLaks 2013-03-06 22:00:07

回答

1

最後的代碼你在做什麼是對發送保存提示的響應,並進行發送的響應重定向。您只能在一個響應中發送一個或另一個。

您可以改爲從新窗口打開要下載的頁面,然後將呼叫頁面重定向到~/Default.aspx