0

我有以下代码。它创建并保存了一个包含 grdview4 内容的 excel 文件,这很棒。但是在应用程序结束时,它会显示一个我不想要的对话框。有没有办法让对话框不显示?

        Response.ContentEncoding = System.Text.Encoding.Unicode
        Response.ContentType = "application/vnd.ms-excel"

        '  Response.AddHeader("content-disposition", "attachment;filename=""C:\EchoBACs\RejectedBACSBACs\rejectedBacs" & NewDate & ".xls")

        '  Response.TransmitFile("C:\rejectedBacs" & NewDate & ".xls")
        Response.Charset = ""
        Me.EnableViewState = False
        Dim oStringWriter As New System.IO.StringWriter
        Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
        GridView4.HeaderRow.Style.Add("background-color", "#FFFFFF")

        GridView4.RenderControl(oHtmlTextWriter)
        Dim helpme As String = oStringWriter.ToString()
      '  Response.Write(oStringWriter.ToString())

        ' HttpContext.Current.Response.End()
        File.WriteAllText("C:\EchoBACs\RejectedBACS\rejectedBacs" & NewDate & ".xls", helpme)

        oHtmlTextWriter.Close()
4

1 回答 1

0

在这种情况下不要使用响应,这将询问另存为对话框。如果您想在没有任何提示的情况下将文件保存到磁盘,请执行以下操作。我用 C# 给你

System.Web.UI.WebControls.DataGrid grid = new System.Web.UI.WebControls.DataGrid();
grid.HeaderStyle.Font.Bold = true;
grid.DataSource = datatable;
grid.DataMember = datatable.TableName;
grid.DataBind();
 using(StreamWriter sw = new StreamWriter ("d:\\temp\test.xls"))
 {
     using (HtmlTextWriter hw = new HtmlTextWriter(sw))
     {
           grid.RenderControl(hw);
     }
}
于 2013-11-13T17:30:32.133 回答