在客户端动态创建Excel文件并下载/保存

3
问题: 在ASP.NET Core 1.1中,以下代码的最后三行有什么替代方案或解决方法?在这最后三行中,VS2015会报错:HttpResponse does not contain a definition for OutputStream, Flush(), End() 背景: 在我的ASP.NET Core 1.1应用程序中,我正在使用EPPlus.Core来实现数据即时导出到Excel并下载/保存到客户端。作为一个入门者,我试图模仿以下示例(取自here),但是VS2015没有识别出这段代码的最后3行
public void ExportListUsingEPPlus()
{
    var data = new[]{
                        new{ Name="Ram", Email="ram@techbrij.com", Phone="111-222-3333" },
                        new{ Name="Shyam", Email="shyam@techbrij.com", Phone="159-222-1596" },
                        new{ Name="Mohan", Email="mohan@techbrij.com", Phone="456-222-4569" },
                        new{ Name="Sohan", Email="sohan@techbrij.com", Phone="789-456-3333" },
                        new{ Name="Karan", Email="karan@techbrij.com", Phone="111-222-1234" },
                        new{ Name="Brij", Email="brij@techbrij.com", Phone="111-222-3333" }
                };

    ExcelPackage excel = new ExcelPackage();
    var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
    workSheet.Cells[1, 1].LoadFromCollection(data, true);
    using (var memoryStream = new MemoryStream())
    {
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.Headers.Add("content-disposition", "attachment;  filename=Contact.xlsx");
        excel.SaveAs(memoryStream);
        memoryStream.WriteTo(Response.OutputStream);
        Response.Flush();
        Response.End();
    }
}

你一直在用不同方式问相同的问题(有时是csv,现在是excel文件)。如果它可以接受一个"Stream",你总是可以使用new ExcelPackage(HttpContext.Response.Body)。只要流的写入是单向的(不可倒回,因为在大多数情况下,你正在直接向用户浏览器的通信口/TCP连接上写入),而且(至少是非官方的)EPPlus包确实具有那个构造函数(请参见源代码此处),你可能需要调用Save()方法。 - Tseng
或者直接执行 excel.SaveAs(HttpContext.Response.Body) 而不需要使用 MemoryStream。为什么要浪费内存,当你可以直接写入响应流呢... 你只需要理解之前解决问题的方法,并在未来应用它们,而不是每次都要求复制粘贴的代码。 - Tseng
1个回答

2

在控制器操作中,您可以返回以下其中之一的FileStreamResult

它将返回指定文件流中的文件,使用指定的内容类型作为Content-Type,并使用指定的文件下载名称作为建议的文件名。

public virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName)

编辑:

示例操作方法-

var data = new[]{
                        new{ Name="Ram", Email="ram@techbrij.com", Phone="111-222-3333" },
                        new{ Name="Shyam", Email="shyam@techbrij.com", Phone="159-222-1596" },
                        new{ Name="Mohan", Email="mohan@techbrij.com", Phone="456-222-4569" },
                        new{ Name="Sohan", Email="sohan@techbrij.com", Phone="789-456-3333" },
                        new{ Name="Karan", Email="karan@techbrij.com", Phone="111-222-1234" },
                        new{ Name="Brij", Email="brij@techbrij.com", Phone="111-222-3333" }
                };

            ExcelPackage excel = new ExcelPackage();
            var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
            workSheet.Cells[1, 1].LoadFromCollection(data, true);

            //var memoryStream = new MemoryStream(excel.GetAsByteArray());

            //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            //Response.Headers.Add("content-disposition", "attachment;  filename=Contact.xlsx");
            //excel.SaveAs(memoryStream);
            //memoryStream.WriteTo(Response.OutputStream);
            //Response.Flush();
            //Response.End();

            return File(excel.GetAsByteArray(), "application/vnd.ms-excel", "Contact.xlsx");

生成的Excel截图-

在此输入图片描述


根据您的建议,它出现了错误:无法访问已关闭的流。这可能是由于 Using{...} 语句在结尾处关闭了流。但是在我删除了 Using{...} 后,它现在下载一个没有数据的 Excel 文件。我已经用 return File(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Contact.xlsx"); 替换了最后三行。 - nam
尝试这个 - new MemoryStream(excel.GetAsByteArray()); - Sanket
我使用了var memoryStream = new MemoryStream(excel.GetAsByteArray());,然后在行 excel.SaveAs(memoryStream); 出现了错误:无法访问已关闭的流 - nam
您无需调用 .SaveAs 因为 .GetAsByteArray 内部已经完成了此操作。 - Sanket
我可能不会按照你期望的方式进行测试。在你那边似乎是可以工作的。能否请你复制/粘贴我的代码,并根据我们迄今为止的讨论进行修改?我会尝试并告诉你是否有效。 - nam
@Sunket,它真的起作用了。谢谢你抽出时间来帮助他人。我注意到你正在使用 application/vnd.ms-excel 而不是 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet。你知道它们之间有什么区别吗? - nam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接