流式处理事件后的.xlsx文件损坏

本文关键字:xlsx 文件 损坏 处理事件 | 更新日期: 2024-09-24 09:52:38

我正在使用EPPlus库创建一个.xlsx文件,然后将其流式传输到浏览器。要流式传输我正在使用的代码:

HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.MapPath("xls/"+ download_results_filename));
            HttpContext.Current.Response.TransmitFile(Server.MapPath("xls/" + download_results_filename));
            HttpContext.Current.Response.Flush();

我的问题是。如果我将代码与库一起使用,保存并打开文档,一切都很好。然而,当我在服务器上创建文件并使用上面的代码将其流式传输给用户时,我会收到一条损坏的文件消息,该消息具有更正或恢复文件的额外功能,我这样做了,文件显示正确;因此,如果使用上面的八位位组流方法将文件流式传输给用户会损坏文件,我应该如何将二进制数据流式传输到用户。我想将内容类型保持为"应用程序/八位字节流",就好像我对它是一个在iPad上遇到问题的excel电子表格一样。

感谢

流式处理事件后的.xlsx文件损坏

您看到这个例子了吗?

http://epplus.codeplex.com/wikipage?title=WebapplicationExample

它使用这条线

Response.BinaryWrite(pck.GetAsByteArray());