导出到Excel不下载excel文件

时间:2016-05-15 18:02:16

标签: c# excel

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

 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();
            }

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

  

长度=   '((System.Web.HttpResponseStream)Response.OutputStream).Length'threw   “System.NotSupportedException”类型的例外

     

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

     

ReadTimeout ='Response.OutputStream.ReadTimeout'抛出异常   类型为'System.InvalidOperationException'

     

WriteTimeout ='Response.OutputStream.WriteTimeout'抛出异常   类型为'System.InvalidOperationException'

0 个答案:

没有答案