0

我有一些代码可以创建一个 Excel 表格,然后提示用户将其保存在某处,并将其附加到电子邮件中并将其发送到某处。当我有 Response.end() 时一切正常,但我想删除它以重定向到新页面。当我删除它并用 response.redirect 替换它时,它不会提示用户保存它。任何想法为什么这不再提示保存?

创建/保存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")
4

1 回答 1

1

您正在做的是发送保存提示的响应,并发送重定向响应。您只能在一个响应中发送一个或另一个。

相反,您可以做的是在新选项卡/窗口中打开您正在下载的页面,然后将调用页面重定向到~/Default.aspx.

于 2013-03-06T22:03:23.200 回答