即时创建 Excel 文件并在客户端上拥有它 download/save
Creating an Excel file on the fly and have it download/save on the client
问题:ASP.NET Core 1.1 and/or 中以下代码的最后三行的替代方法是什么 and/or 解决方法是什么?在最后三行中 VS2015
抱怨 HttpResponse does not contain a definition for OutputStream, Flush(), End()
背景:在我的 ASP.NET Core 1.1
应用程序中,我正在使用 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),但 VS2015
无法识别 last 3 lines
这段代码。
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();
}
}
您可以在控制器操作中 return FileStreamResult 之一。
它return是指定 fileStream 中的一个文件,指定的 contentType 作为 Content-Type,指定的 fileDownloadName 作为建议的文件名。
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 屏幕截图-
问题:ASP.NET Core 1.1 and/or 中以下代码的最后三行的替代方法是什么 and/or 解决方法是什么?在最后三行中 VS2015
抱怨 HttpResponse does not contain a definition for OutputStream, Flush(), End()
背景:在我的 ASP.NET Core 1.1
应用程序中,我正在使用 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),但 VS2015
无法识别 last 3 lines
这段代码。
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();
}
}
您可以在控制器操作中 return FileStreamResult 之一。
它return是指定 fileStream 中的一个文件,指定的 contentType 作为 Content-Type,指定的 fileDownloadName 作为建议的文件名。
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 屏幕截图-