付出与汲取

C# Web 下载excel文件后直接导入下载的文件报错的问题

下载代码如下:

System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel.sheet.binary.macroEnabled.12";
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + outputFileName);
System.Web.HttpContext.Current.Response.BinaryWrite(oByte);
//清除缓存
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();

上面标红的代码为出错的关键所在,应改为:

System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";

posted on 2020-01-10 14:48  付出与汲取  阅读(507)  评论(0编辑  收藏  举报

导航