ASP.NET MVC使用EPPlus,导出数据到Excel中(2)

public class HomeController : Controller { // GET: Home public ActionResult Index() { StudentViewModel model = new StudentViewModel(); return View(model); } public FileContentResult ExportToExcel() { List<Student> lstStudent = StaticDataOfStudent.ListStudent; string[] columns = { "ID", "Name","Age"}; byte[] filecontent = ExcelExportHelper.ExportExcel(lstStudent,"", false, columns); return File(filecontent, ExcelExportHelper.ExcelContentType, "MyStudent.xlsx"); } }

我们的视图代码:

@model ExportToExcel.Models.StudentViewModel @{ ViewBag.Title = "Excel文件导出"; } <div> <div> <a href="https://www.jb51.net/@Url.Action("ExportToExcel")">Export</a> </div> <div> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Sex</th> <th>Age</th> <th>Email</th> </tr> </thead> <tbody> @foreach (var item in Model.ListStudent) { <tr> <td>@item.ID</td> <td>@item.Name</td> <td>@item.Sex</td> <td>@item.Age</td> <td>@item.Email</td> </tr> } </tbody> </table> </div> </div>

效果图:

ASP.NET MVC使用EPPlus,导出数据到Excel中

点击Export之后,就导出了Excel文件到浏览器中:打开之后。

ASP.NET MVC使用EPPlus,导出数据到Excel中

总结:这个导出帮助类,可以定制导出那些列。

string[] columns = { "ID", "Name","Age"}; byte[] filecontent = ExcelExportHelper.ExportExcel(lstStudent,"", false, columns); return File(filecontent, ExcelExportHelper.ExcelContentType, "MyStudent.xlsx");

这里我只是导出这三列。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjyygf.html