EPPLus-导出到Excel返回失败的网络错误 [英] EPPLus - export to Excel returns failed-network error

查看:400
本文介绍了EPPLus-导出到Excel返回失败的网络错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET Core 1.1应用程序中,我正在使用 EPPLus.Core 将数据导出到Excel。

In my ASP.NET Core 1.1 app, I am exporting data to Excel using EPPLus.Core. But at the time of File download I get the error: Failed- network error

控制器

public async Task<FileStreamResult> CPT_RC_Excel(int FiscalYear)
{
    var custlist = _Context.Customers.Where(c => c.Region=="NW").ToList();

  MemoryStream ms = new MemoryStream();
  using (ExcelPackage pck = new ExcelPackage(ms))
  {
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Clients");
    ws.Cells["A1"].LoadFromCollection(custlist);

    Response.Clear();
    Response.Headers.Add("content-disposition", "attachment;  filename=TestFile.xlsx");
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    var bytes = pck.GetAsByteArray();
    await Response.Body.WriteAsync(bytes, 0, bytes.Length);
    return File(ms, "application/force-download", "TestFile.xlsx");
  }
}

UPDATE


  1. Google Chrome和IE 11 均发生错误。整个应用程序都在本地桌面上。这里没有网络驱动器。

  2. 错误似乎与上面的代码有关,因为其他操作方法(代码显示为在同一控制器中)确实可以成功下载csv /文本文件。

  1. Error occurs on both Google Chrome and IE 11. The entire app is on a local desktop. No network drive is involved here.
  2. Error does seem to be related to the code above since another action method (code shown here) in the same controller does down load the csv/text files successfully.


推荐答案

您可以尝试以下操作:

public async Task<ActionResult> CPT_RC_Excel(int FiscalYear)
        {

            var fileContentAsByteArray = [generate your file as a byte array]

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

        }

如果这不起作用,则问题出在

if this doesn't work then the issue is with the file generation.

尝试删除对MemoryStream的引用并使用...

try removing the reference to the MemoryStream and use...

...
using (var excelPackage = new ExcelPackage())
{
...
}

这篇关于EPPLus-导出到Excel返回失败的网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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