Mvc 5中匯出Excel

風靈使發表於2018-05-10

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>

執行結果如圖:

這裡寫圖片描述

這裡寫圖片描述

相關文章