加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ASP.NET MVC – 从MemoryStream下载Excel文件(损坏文件)

发布时间:2020-12-15 19:26:56 所属栏目:百科 来源:网络整理
导读:我正在尝试使用浏览器中的内置下载来下载Excel文件.在我在控制器中创建Excel文件后,基本上下载工作正常,但是当我尝试在Excel中打开此文件时,我被告知该文件已损坏: Excel cannot open the file ‘Report.xlsx’ because the file format or file extension
我正在尝试使用浏览器中的内置下载来下载Excel文件.在我在控制器中创建Excel文件后,基本上下载工作正常,但是当我尝试在Excel中打开此文件时,我被告知该文件已损坏:

Excel cannot open the file ‘Report.xlsx’ because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

强制文件在Excel中打开时,工作表只是空的.我不知道为什么会这样.

我没有展示我是如何创建Excel文件的,因为这是无关紧要的(它在使用内存流之前工作正常).

var stream = wBook.WriteXLSX(); // Return a MemoryStream (Using DTG.Spreadsheet)

return File(stream,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","Report.xlsx");

解决方法

您应将当前流位置设置为0以成功导出文件.

var stream = wBook.WriteXLSX(); // Return a MemoryStream (Using DTG.Spreadsheet)

stream.Seek(0,SeekOrigin.Begin);

return File(stream,"Report.xlsx");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读