通过 HttpContext 发送 EPPLus Excel 文件
Sending EPPLus Excel file via HttpContext
我正在为我的 .NET Core 3.1 应用程序使用 EPPLus 库;目前我正在尝试实现一个简单的 Export
函数,它根据给定的数据生成 sheet 并立即将其下载到用户 PC。
我有以下内容:
public void Export(ProductionLine productionLine, HttpContext context)
{
using (var package = new ExcelPackage())
{
var ws = package.Workbook.Worksheets.Add("MySheet");
ws.Cells["A1"].Value = "This is cell A1";
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
context.Response.Headers.Add(
"content-disposition",
string.Format("attachment; filename={0}", "MySheet.xlsx"));
context.Response.SendFileAsync(package);
}
}
HttpContext
是通过一个简单地调用 HttpContext 控制器基础的控制器给出的。 HttpContext 基于 Microsoft.AspNetCore.Http
我目前遇到的错误是 cannot convert from 'OfficeOpenXml.ExcelPackage' to 'Microsoft.Extensions.FileProviders.IFileInfo'
合乎逻辑的,但我认为将文件更改为 IFileInfo
是不可能的。
是否有另一种通过 HttpContextResponse 发送 EPPlus 文件的方法?
折腾了一下,好像还是用return File()
功能更方便。我重做了 Export
函数,如下所示:
public object Export(ProductionLine productionLine, HttpContext context)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
FileInfo fileName = new FileInfo("ExcellData.xlsx");
using (var package = new OfficeOpenXml.ExcelPackage(fileName))
{
var ws = package.Workbook.Worksheets.Add("MySheet");
ws.Cells["A1"].Value = "This is cell A1";
MemoryStream result = new MemoryStream();
result.Position = 0;
package.SaveAs(result);
return result;
}
我的控制器是这样的:
public IActionResult ExportCSV([FromQuery] string Orderno)
{
try
{
ProductionLine prodLine = _Prodline_Service.GetAllByOrderno(Orderno);
MemoryStream result = (MemoryStream)_ExcelExportService.Export(prodLine, HttpContext);
// Set memorystream position; if we don't it'll fail
result.Position = 0;
return File(result, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
} catch(Exception e)
{
Console.WriteLine(e);
return null;
}
}
我正在为我的 .NET Core 3.1 应用程序使用 EPPLus 库;目前我正在尝试实现一个简单的 Export
函数,它根据给定的数据生成 sheet 并立即将其下载到用户 PC。
我有以下内容:
public void Export(ProductionLine productionLine, HttpContext context)
{
using (var package = new ExcelPackage())
{
var ws = package.Workbook.Worksheets.Add("MySheet");
ws.Cells["A1"].Value = "This is cell A1";
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
context.Response.Headers.Add(
"content-disposition",
string.Format("attachment; filename={0}", "MySheet.xlsx"));
context.Response.SendFileAsync(package);
}
}
HttpContext
是通过一个简单地调用 HttpContext 控制器基础的控制器给出的。 HttpContext 基于 Microsoft.AspNetCore.Http
我目前遇到的错误是 cannot convert from 'OfficeOpenXml.ExcelPackage' to 'Microsoft.Extensions.FileProviders.IFileInfo'
合乎逻辑的,但我认为将文件更改为 IFileInfo
是不可能的。
是否有另一种通过 HttpContextResponse 发送 EPPlus 文件的方法?
折腾了一下,好像还是用return File()
功能更方便。我重做了 Export
函数,如下所示:
public object Export(ProductionLine productionLine, HttpContext context)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
FileInfo fileName = new FileInfo("ExcellData.xlsx");
using (var package = new OfficeOpenXml.ExcelPackage(fileName))
{
var ws = package.Workbook.Worksheets.Add("MySheet");
ws.Cells["A1"].Value = "This is cell A1";
MemoryStream result = new MemoryStream();
result.Position = 0;
package.SaveAs(result);
return result;
}
我的控制器是这样的:
public IActionResult ExportCSV([FromQuery] string Orderno)
{
try
{
ProductionLine prodLine = _Prodline_Service.GetAllByOrderno(Orderno);
MemoryStream result = (MemoryStream)_ExcelExportService.Export(prodLine, HttpContext);
// Set memorystream position; if we don't it'll fail
result.Position = 0;
return File(result, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
} catch(Exception e)
{
Console.WriteLine(e);
return null;
}
}