asp.net - 如何在 ASP.NET 和 VB.NET 中使用 Open XML 返回文件
问题描述
我有一个使用 VB.NET 的旧版 ASP.NET 3.5(不是 ASP.NET MVC)应用程序。当用户单击按钮时,我想下载一个 Excel 电子表格。我正在使用 OpenXML 库。我在 ASP.NET MVC 中使用了相同的功能,您可以使用 OpenXML 返回文件,如下所示:-
public FileContentResult GetCollectionsExcel()
{
var collections = CurrentBroker.Collections;
var model = new FinancialCollectionsModel(collections);
var package = new ExcelPackage();
var sheet = package.Workbook.Worksheets.Add("Collections");
return File(package.GetAsByteArray(),"application/vnd.openxmlformatsofficedocument.spreadsheet.sheet", "Collections.xlsx");
}
它在传统的 ASP.NET 和 VB.NET 中是什么等价物?该方法的返回类型应该是什么,因为我们在 ASP.NET 中没有 FileContentResult。
解决方案
Access the response directly, write the data to the response stream and set the necessary headers
'...code removed for brevity
Dim response = HttpContext.Current.Response
response.ContentType = "application/vnd.openxmlformatsofficedocument.spreadsheet.sheet"
response.AppendHeader("Content-Disposition", "attachment; filename=""Collections.xlsx""")
response.BinaryWrite(package.GetAsByteArray())
推荐阅读
- javascript - 获取文本中的点击位置(非输入)
- azureservicebus - 用于唯一客户端消息的 Azure 服务总线唯一会话以及如何建立非代理连接?
- vba - 不回忆变量值
- sql - Redshift 表创建不持久/不持久
- vba - 多台计算机将数据添加到一个文档
- django - 在 Django REST 序列化程序中丢失外键值
- android - 显示为正方形的浮动操作按钮
- scala - 在同一个实例上多次执行 IO::unsafeRunSync 是否合适?
- android - 检查调用 stopForeground(true)
- php - 使用 symfony2 在 mysql 中进行三级深度查询