最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET MVC把表格導出到Excel

 更新時間:2022年07月31日 14:23:38   作者:Darren Ji  
這篇文章介紹了ASP.NET 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")

到此這篇關于ASP.NET MVC把表格導出到Excel的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

于都县| 海盐县| 阳山县| 工布江达县| 奎屯市| 吉木乃县| 西乌| 阳信县| 扎鲁特旗| 乡宁县| 新营市| 眉山市| 青浦区| 利津县| 阿荣旗| 万安县| 驻马店市| 石景山区| 新野县| 刚察县| 修水县| 麻阳| 乐清市| 德令哈市| 海口市| 乌兰县| 永善县| 融水| 潮安县| 于都县| 邯郸县| 香河县| 扎赉特旗| 永善县| 西平县| 仙游县| 茌平县| 肃北| 黔西| 桂平市| 津南区|