EPPLus - export to Excel returns failed-network error

我的梦境 提交于 2019-12-25 11:53:18

问题


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

Controller

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. 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.

回答1:


You could try something like this:

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.

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

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


来源:https://stackoverflow.com/questions/44337947/epplus-export-to-excel-returns-failed-network-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!