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>
執行結果如圖:
相關文章
- Spring學習手冊 2:Spring MVC 匯出excel表格SpringMVCExcel
- java spring mvc jsp excel 匯入JavaSpringMVCJSExcel
- 匯出excelExcel
- vue excel匯入匯出VueExcel
- ASP.NET MVC 匯入Excel檔案ASP.NETMVCExcel
- Java匯出ExcelJavaExcel
- Vue匯出ExcelVueExcel
- PHP 匯出 ExcelPHPExcel
- PHP匯出EXCELPHPExcel
- java匯出Excel定義匯出模板JavaExcel
- Excel模板匯出之動態匯出Excel
- Angular Excel 匯入與匯出AngularExcel
- Excel匯出實列Excel
- js匯出Excel表格JSExcel
- vue 前端匯出 excelVue前端Excel
- poi的excel匯出Excel
- vue匯出Excel表格VueExcel
- Excel優雅匯出Excel
- vue + element + 匯入、匯出excel表格VueExcel
- kxcel, 方便匯入和匯出 ExcelExcel
- Vue框架下實現匯入匯出Excel、匯出PDFVue框架Excel
- 騰訊文件怎樣匯出excel表格 騰訊文件如何匯出excelExcel
- element-ui 匯出excelUIExcel
- springboot poi匯出excel表格Spring BootExcel
- excel匯出、mysql分頁ExcelMySql
- Go 使用反射匯出 ExcelGo反射Excel
- Laravel Excel3.0匯出LaravelExcel
- oracle 匯出excel 格式整改OracleExcel
- java匯出Excel檔案JavaExcel
- PhpSpreadsheet 匯出excel範例PHPExcel
- 如何使用 JavaScript 匯入和匯出 ExcelJavaScriptExcel
- EasyPoi, Excel資料的匯入匯出Excel
- Vue + Element 實現匯入匯出ExcelVueExcel
- laravel-excel匯出excel下拉選擇框LaravelExcel
- 前端實現Excel匯入和匯出功能前端Excel
- NPOI匯出和匯入Excel,Word和PDFExcel
- Java之POI操作Excel表-匯入匯出JavaExcel
- 基於 PhpSpreadsheet 簡單 Excel 匯入匯出PHPExcel