0

我创建了 excel 表并使用保存

xlWorkBook.SaveCopyAs("abc.xls");

然后我正在使用

 byte[] byteArray = File.ReadAllBytes("abc.xls");
 File.Delete("abc.xls");
 Response.ContentType = "application/vnd.ms-excel";
 Response.AppendHeader("Content-Disposition", "attachment; filename=abc.xls");
 Response.BinaryWrite(byteArray);
 Response.End();

发送 Excel 工作表作为响应。

但问题出在客户端,它首先提示保存 Excel 工作簿“Book1”,然后提示保存“abc.xls”。如何防止这种情况?

早些时候我正在使用

xlWorkBook.SaveAs("abc.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

它工作正常(直接提示保存“abc.xls”)。但我改为 xlWorkBook.SaveCopyAs()因为xlWorkBook.SaveAs()在 IIS 中部署时抛出异常

4

1 回答 1

0

我有我的代码

    string fileName = "xyz.xls";

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
    HttpContext.Current.Response.ContentType = "application/vnd.xls";           

    /*      
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            //extract
        }
    }
    */


    HttpContext.Current.Response.Write(sw.ToString());
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();
于 2012-12-14T12:20:40.183 回答