.net mvc epplus导出excel
时间: 2023-05-15 20:05:05 浏览: 75
可以使用以下代码来导出 Excel 文件:
```csharp
public ActionResult ExportToExcel()
{
var package = new ExcelPackage();
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
// Add data to worksheet
worksheet.Cells[1, 1].Value = "Name";
worksheet.Cells[1, 2].Value = "Age";
worksheet.Cells[2, 1].Value = "John";
worksheet.Cells[2, 2].Value = 30;
worksheet.Cells[3, 1].Value = "Jane";
worksheet.Cells[3, 2].Value = 25;
// Auto fit columns
worksheet.Cells.AutoFitColumns();
// Set response headers
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=export.xlsx");
// Write file to response
Response.BinaryWrite(package.GetAsByteArray());
Response.End();
return null;
}
```
这段代码使用 EPPlus 库来创建 Excel 文件,并将其写入 HTTP 响应中,以便用户可以下载该文件。
阅读全文