MVC把表格导出到Excel

有关Model:

namespace MvcApplication1.Models
{
    public class Coach
    {
        public  int Id { get; set; }
        public string Name { get; set; }
    }
}

HomeController中,借助GridView控件把内容导出到Excel:

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(GetCoaches());
        }

        private List<Coach> GetCoaches()
        {
            return new List<Coach>()
            {
                new Coach(){Id = 1, Name = "斯科拉里"},
                new Coach(){Id = 2, Name = "米西维奇"}
            };
        }

        public void ExportClientsListToExcel()
        {
            var grid = new System.Web.UI.WebControls.GridView();

            grid.DataSource = from item in GetCoaches()
                              select new
                              {
                                  编号 = item.Id,
                                  主教练 = item.Name
                              };

            grid.DataBind();

            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=Exported_Coaches.xls");
            Response.ContentType = "application/excel";
            Response.Charset = "utf-8";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            grid.RenderControl(htw);

            Response.Write(sw.ToString());

            Response.End();

        }

    }
}

Home/Index.cshtml强类型集合视图:

@model IEnumerable<MvcApplication1.Models.Coach>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<table>
    <tr>
        <th>编号</th>
        <th>主教练</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.Id</td>
            <td>@item.Name</td>
        </tr>
    }
</table>

<br/>
@Html.ActionLink("导出到Excel","ExportClientsListToExcel")

MVC把表格导出到Excel

时间: 2024-10-06 22:37:59

MVC把表格导出到Excel的相关文章

将HTML表格导出到EXCEL,兼容Firefox,支持中文

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <meta http-equiv="Content

html页面表格导出到excel

几种把html页面表格导出到excel的方法比较. 表格例子如下: <table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td colspan="5" align="center">html 表格导出道Exceltd

JS 将页面上的表格导出为 Excel 文件

如果在页面上展示了一个表格,想把这个表格导出为Excel文件,那么在要求不高的情况下,可以直接利用 JavaScript 的 Blob 和 Object URL 特性将表格导出.不过,这就是利用了 Excel 能打开 HTML 文档的特性,所以导出的表格实际上是一个 HTML 文档,并且其扩展名只能为 .xls,而不能是 .xlsx,否则Excel无法打开.(有使用JavaScript生成真正Excel文件的方案,以后再研究.) 实例: <!DOCTYPE html> <html>

html页面表格导出到excel总结

转载:http://www.cnblogs.com/liuguanghai/archive/2012/12/31/2840262.html <table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td colspan="5" align=&qu

将表格导出为excel

1 <table id="tableExcel" border="1"> 2 <tr> 3 <th>零</th> 4 <th>一</th> 5 <th>二</th> 6 <th>三</th> 7 <th>四</th> 8 </tr> 9 <tr> 10 <td>万籁寂无声</td&g

mysql数据库表格导出为excel表格

在本地数据库中操作如下: 由于excel表格的编码是GBK,所以导出时要加一个设置字符编码: select * from 某个表 into outfile 'd:/文件名.xls' CHARACTER SET gbk;

ssh+jxl将表格导出为Excel

首先准备jxl.jar. 然后让我们了解了解这个jxl,Excel本来就有工作簿.工作表.单元格等属性,所以我们从数据库导出表格也必须先有工作簿(workbook).工作表(sheet).单元格(label).实现导出表格也是从三个入手. 首先创建一个writeworkbook对象 Writableworkbook  book=Workbook.createWorkbook(new File(path)): 然后创建工作表sheet对象 WritableSheet sheet=book.crea

前端JS脚本将网页表格导出为Excel

话不多说,上代码! <!DOCTYPE> <html> <head> <title>Excel Test</title> </head> <body> <div style="width:100%;padding:40px;"> Excel Test </div> <table id="excel"> <tr> <td>Na

mvc项目,导出到Excel,中文显示乱码

1 public class HomeController : Controller 2 { 3 static List<User> GetUsers() 4 { 5 List<User> list = new List<User>() { 6 new User{Id=1,Name="张三"}, 7 new User{Id=2,Name="lisi"}, 8 new User{Id=3,Name="wangwu"