Mvc 5中匯出Excel
Model層
Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ExportToExcel.Models
{
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
}
StaticDataOfStudent.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ExportToExcel.Models
{
/// <summary>
/// 返回靜態的資料
/// </summary>
public class StaticDataOfStudent
{
public static List<Student> ListStudent
{
get
{
return new List<Student>()
{
new Student(){ID=1,Name="曹操",Sex="男",Email="caocao@163.com",Age=24},
new Student(){ID=2,Name="李易峰",Sex="女",Email="lilingjie@sina.com.cn",Age=24},
new Student(){ID=3,Name="張三丰",Sex="男",Email="zhangsanfeng@qq.com",Age=224},
new Student(){ID=4,Name="孫權",Sex="男",Email="sunquan@163.com",Age=1224},
};
}
}
}
}
StudentViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ExportToExcel.Models
{
public class StudentViewModel
{
public List<Student> ListStudent
{
get
{
return StaticDataOfStudent.ListStudent;
}
}
}
}
HomeController.cs
using ExportToExcel.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ExportToExcel.Controllers
{
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");
}
}
}
Index.cshtml
@model ExportToExcel.Models.StudentViewModel
@{
ViewBag.Title = "Excel檔案匯出";
}
<div class="panel">
<div class="panel-heading">
<a href="@Url.Action("ExportToExcel")" class="btn btn-primary">匯出</a>
</div>
<div class="panel-body">
<table class="table table-striped table-bordered">
<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>
執行結果如圖:
相關文章
- 匯出excelExcel
- vue excel匯入匯出VueExcel
- js匯出EXCELjs匯出EXCELJSExcel
- Spring學習手冊 2:Spring MVC 匯出excel表格SpringMVCExcel
- PHP 匯出 ExcelPHPExcel
- PHP匯出EXCELPHPExcel
- Vue匯出ExcelVueExcel
- Java匯出ExcelJavaExcel
- php 匯出excelPHPExcel
- POI 匯出ExcelExcel
- java spring mvc jsp excel 匯入JavaSpringMVCJSExcel
- ASP.NET MVC 匯入Excel檔案ASP.NETMVCExcel
- java匯出Excel定義匯出模板JavaExcel
- Excel模板匯出之動態匯出Excel
- Angular Excel 匯入與匯出AngularExcel
- 騰訊文件怎樣匯出excel表格 騰訊文件如何匯出excelExcel
- Java 通過Xml匯出Excel檔案,Java Excel 匯出工具類,Java匯出Excel工具類JavaXMLExcel
- js匯出Excel表格JSExcel
- vue匯出Excel表格VueExcel
- Excel優雅匯出Excel
- Excel匯出實列Excel
- Springmvc匯出excelSpringMVCExcel
- appfuse:Excel匯出APPExcel
- DataGridView匯出ExcelViewExcel
- poi的excel匯出Excel
- vue 前端匯出 excelVue前端Excel
- kxcel, 方便匯入和匯出 ExcelExcel
- vue + element + 匯入、匯出excel表格VueExcel
- 轉java操作excel匯入匯出JavaExcel
- Vue框架下實現匯入匯出Excel、匯出PDFVue框架Excel
- Laravel Excel3.0匯出LaravelExcel
- Go 使用反射匯出 ExcelGo反射Excel
- java匯出Excel檔案JavaExcel
- excel匯出、mysql分頁ExcelMySql
- element-ui 匯出excelUIExcel
- oracle 匯出excel 格式整改OracleExcel
- java poi 匯出excel加密JavaExcel加密
- php匯出資料excelPHPExcel