2013-03-06 41 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