.net mvc epplus导出excel文档
时间: 2023-05-16 20:06:52 浏览: 148
ASP.NET MVC使用EPPlus,导出数据到Excel中
您可以使用EPPlus库来导出Excel文档。首先,您需要在您的项目中安装EPPlus库。然后,您可以使用以下代码来导出Excel文档:
```csharp
using OfficeOpenXml;
using System.IO;
public void ExportToExcel()
{
// Create a new Excel package
using (var package = new ExcelPackage())
{
// Add a new worksheet to the Excel package
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
// Add some data to the worksheet
worksheet.Cells["A1"].Value = "Hello";
worksheet.Cells["B1"].Value = "World";
// Save the Excel package to a stream
var stream = new MemoryStream();
package.SaveAs(stream);
// Set the stream position to the beginning
stream.Position = 0;
// Return the Excel file as a byte array
var bytes = stream.ToArray();
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx");
Response.BinaryWrite(bytes);
Response.End();
}
}
```
这个例子创建了一个包含两列数据的Excel文档,并将其作为字节数组返回给客户端。您可以根据您的需求修改这个例子。
阅读全文