快速创建Excel文件,并在客户端上下载/保存 [英] Creating an Excel file on the fly and have it download/save on the client

查看:90
本文介绍了快速创建Excel文件,并在客户端上下载/保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:ASP.NET Core 1.1中以下代码的后三行的替代方案是什么和/或解决方法是什么?在这最后三行中, VS2015 抱怨 HttpResponse不包含OutputStream,Flush(),End()

的定义

背景:在我的 ASP.NET Core 1.1 应用中,我正在使用

Question: What is an alternative to the last three lines of the following code in ASP.NET Core 1.1 and/or what are workarounds? On these last three lines VS2015 is complaining HttpResponse does not contain a definition for OutputStream, Flush(), End()

Background: In my ASP.NET Core 1.1 app I'm using EPPlus.Core to export data on the fly to Excel and have it downloaded/save on the client side. As a starter, I'm trying to mimic the following example (taken from here), but VS2015 is not recognizing the last 3 lines of this code.

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

解决方案

You can return one of FileStreamResult in Controller action.

It returns a file in the specified fileStream with the specified contentType as the Content-Type and the specified fileDownloadName as the suggested file name.

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


EDIT:

Sample action method-

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");

Generated Excel screenshot-

这篇关于快速创建Excel文件,并在客户端上下载/保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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