.net mvc epplus导出excel
时间: 2023-05-15 09:05:01 浏览: 107
可以使用以下代码来使用 EPPlus 在 .NET MVC 中导出 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 the Excel package to the response stream
Response.BinaryWrite(package.GetAsByteArray());
Response.End();
return null;
}
```
这段代码将在服务器上创建一个 Excel 文件,并将其作为响应发送到客户端。请注意,您需要在您的项目中安装 EPPlus 包。