在 ASP.NET MVC 中下载 Excel 文件通常有几种常见方式,以下我将介绍 最常用且推荐 的两种方法:
- 使用
FileResult直接返回字节数组或文件路径(适用于已生成的 Excel 文件) - 使用 NPOI 或 EPPlus 动态生成 Excel 并返回(适用于从数据库数据动态生成)
✅ 方法一:返回已存在的 Excel 文件(最简单)
假设你的 Excel 文件已经保存在服务器某个路径下,/Content/Reports/report.xlsx
Controller 代码:
using System.IO;
using System.Web.Mvc;
public class DownloadController : Controller
{
public ActionResult DownloadExcel()
{
string filePath = Server.MapPath("~/Content/Reports/report.xlsx");
if (!System.IO.File.Exists(filePath))
{
return HttpNotFound();
}
// 返回文件下载
return File(filePath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "report.xlsx");
}
}
前端链接:
<a href="@Url.Action("DownloadExcel", "Download")">下载 Excel</a>
✅ 方法二:动态生成 Excel 并返回(推荐用于数据导出)
使用 NPOI 库(免费、支持 .xlsx 和 .xls)来动态生成 Excel 文件。
安装 NPOI NuGet 包
Install-Package NPOI
Controller 代码
using System; using System.Collections.Generic; using System.IO; using System.Web.Mvc; usingNPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; public class ExportController : Controller { public ActionResult ExportToExcel() { // 模拟数据 var dataList = new List<ExportData> { new ExportData { Name = "张三", Age = 25, City = "北京" }, new ExportData { Name = "李四", Age = 30, City = "上海" }, new ExportData { Name = "王五", Age = 28, City = "广州" } }; // 创建 Excel 工作簿 IWorkbook workbook = new XSSFWorkbook(); // .xlsx 格式 ISheet sheet = workbook.CreateSheet("数据表"); // 创建表头 IRow headerRow = sheet.CreateRow(0); headerRow.CreateCell(0).SetCellValue("姓名"); headerRow.CreateCell(1).SetCellValue("年龄"); headerRow.CreateCell(2).SetCellValue("城市"); // 填充数据 for (int i = 0; i < dataList.Count; i++) { IRow dataRow = sheet.CreateRow(i + 1); dataRow.CreateCell(0).SetCellValue(dataList[i].Name); dataRow.CreateCell(1).SetCellValue(dataList[i].Age); dataRow.CreateCell(2).SetCellValue(dataList[i].City); } // 将工作簿写入内存流 using (var memoryStream = new MemoryStream()) { workbook.Write(memoryStream); memoryStream.Position = 0; // 返回文件下载 return File( memoryStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "导出数据.xlsx" ); } } } // 数据模型 public class ExportData { public string Name { get; set; } public int Age { get; set; } public string City { get; set; } }
前端链接:
<a href="@Url.Action("ExportToExcel", "Export")">导出 Excel</a>
✅ 方法三:使用 EPPlus(更强大的 Excel 操作库)
EPPlus 功能更强大,支持样式、图表等,但注意 EPPlus 5+ 需要商业许可证,4.x 版本免费。
安装:
Install-Package EPPlus -Version 4.5.3.3
Controller 代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;
using OfficeOpenXml;
public class ExportController : Controller
{
public ActionResult ExportWithEPPlus()
{
var dataList = new List<ExportData>
{
new ExportData { Name = "张三", Age = 25, City = "北京" },
new ExportData { Name = "李四", Age = 30, City = "上海" },
new ExportData { Name = "王五", Age = 28, City = "广州" }
};
// 设置 EPPlus 许可证上下文(必须)
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add("数据表");
// 写入表头
worksheet.Cells[1, 1].Value = "姓名";
worksheet.Cells[1, 2].Value = "年龄";
worksheet.Cells[1, 3].Value = "城市";
// 写入数据
for (int i = 0; i < dataList.Count; i++)
{

worksheet.Cells[i + 2, 1].Value = dataList[i].Name;
worksheet.Cells[i + 2, 2].Value = dataList[i].Age;
worksheet.Cells[i + 2, 3].Value = dataList[i].City;
}
// 返回文件
var bytes = package.GetAsByteArray();
return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "导出数据.xlsx");
}
}
}
📌 注意事项
| 项目 | 说明 |
|---|---|
| MIME 类型 | .xlsx 使用 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
| 大文件处理 | 如果数据量很大,建议使用流式写入,避免内存溢出 |
| 中文文件名 | 确保文件名编码正确,必要时使用 HttpUtility.UrlEncode |
| 权限问题 | 确保网站有权限读取服务器文件路径 |
| NPOI vs EPPlus | NPOI 免费且轻量;EPPlus 功能更强但需注意许可证 |
🎯 总结推荐
- 简单文件下载 → 使用
File(filePath, mimeType, fileName) - 动态生成 Excel → 推荐使用 NPOI(免费、稳定)或 EPPlus(功能强大)
- 大数据量 → 考虑使用 ClosedXML 或流式写入
如有具体场景(如从数据库导出、带样式、分页等),可以告诉我,我会提供更详细的代码示例。
首发原创文章,作者:世雄 - 原生数据库架构专家,如若转载,请注明出处:https://idctop.com/article/486074.html



