导出到Excel不下载excel文件 [英] Export to Excel not downloading excel file

查看:124
本文介绍了导出到Excel不下载excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码片段,我在大多数网络应用程序上使用了一段时间,将数据从数据库导出到excel,但似乎对我不起作用了。

I have this code snippet I have been using for a while on most of my web applications to export data from database to excel, but doesn't seem to work for me anymore.

var recDownload = GetDownload();
            var excel = new ExcelPackage();
            var workSheet = excel.Workbook.Worksheets.Add("recDownload");
            var totalCols = recDownload.Columns.Count;
            var totalRows = recDownload.Rows.Count;

            for (var col = 1; col <= totalCols; col++)
            {
                workSheet.Cells[1, col].Value = recDownload.Columns[col - 1].ColumnName;
            }
            for (var row = 1; row <= totalRows; row++)
            {
                for (var col = 0; col < totalCols; col++)
                {
                    workSheet.Cells[row + 1, col + 1].Value = recDownload.Rows[row - 1][col];
                }
            }
            using (var memoryStream = new MemoryStream())
            {
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;  filename=products.xlsx");
                excel.SaveAs(memoryStream);
                memoryStream.WriteTo(Response.OutputStream);
                Response.Flush();
                Response.End();
            }

这是我在调试时得到的错误

This is the error I get while debugging

长度='( (System.Web.HttpResponseStream)Response.OutputStream).Length'抛出类型'System.NotSupportedException'的异常

Length = '((System.Web.HttpResponseStream)Response.OutputStream).Length' threw an exception of type 'System.NotSupportedException'

Position ='((System.Web.HttpResponseStream)Response.OutputStream) .Position'抛出了'System.NotSupportedException'类型的异常

Position = '((System.Web.HttpResponseStream)Response.OutputStream).Position' threw an exception of type 'System.NotSupportedException'

ReadTimeout ='Response.OutputStream.ReadTimeout'引发了类型'System.InvalidOperationException'的异常

ReadTimeout = 'Response.OutputStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'

WriteTimeout ='Response.OutputStream.WriteTimeout'引发类型'System.InvalidOperationException'的异常

WriteTimeout = 'Response.OutputStream.WriteTimeout' threw an exception of type 'System.InvalidOperationException'

推荐答案

你试过没有 MemoryStream ,即'excel.SaveAs(Response.OutputStream)'

Have you tried without MemoryStream, i.e. ‘excel.SaveAs(Response.OutputStream)’?

由于问题也与ASP有关,请检查此资源:
HT tp://forums.asp.net/

并提供有关代码行的详细信息引起异常。


这篇关于导出到Excel不下载excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆