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>

运行结果如图:

这里写图片描述

这里写图片描述

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页