ASP.NET MVC把表格導出到Excel
有關(guān)Model:
namespace MvcApplication1.Models { public class Coach { public int Id { get; set; } public string Name { get; set; } } }
HomeController中,借助GridView控件把內(nèi)容導出到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")
到此這篇關(guān)于ASP.NET MVC把表格導出到Excel的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- .NET6導入和導出EXCEL
- Asp.Net Core實現(xiàn)Excel導出功能的實現(xiàn)方法
- ASP.NET Core 導入導出Excel xlsx 文件實例
- asp.net DataTable導出Excel自定義列名的方法
- ASP.NET使用GridView導出Excel實現(xiàn)方法
- Asp.Net使用Npoi導入導出Excel的方法
- asp.net導出excel的簡單方法實例
- asp.net導出Excel類庫代碼分享
- ASP.NET導出數(shù)據(jù)到Excel的實現(xiàn)方法
- Asp.net中DataTable導出到Excel的方法介紹
- ASP.NET用DataSet導出到Excel的方法
- asp.net GridView導出到Excel代碼
相關(guān)文章
js獲取Treeview選中的節(jié)點(C#選中CheckBox項)
方法網(wǎng)上有很多,試了一下都有瑕疵,于是設(shè)置斷點調(diào)試,各個屬性查找有用的字段,終于找到,接下來與大家分享解決方法,需要了解的朋友可以參考下2012-12-12ASP.NET GridView中文本內(nèi)容無法換行(自動換行/正常換行)
用GridView來顯示課程表,每個單元格的內(nèi)容包括課程名、上課地點、教師姓名,然后我想讓它們分行顯示,感興趣的朋友可以了解下,或許對你有所幫助2013-02-02淺談ASP.NET Core 中jwt授權(quán)認證的流程原理
這篇文章主要介紹了淺談ASP.NET Core 中jwt授權(quán)認證的流程原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03asp.net中IDataParameter調(diào)用存儲過程的實現(xiàn)方法
這篇文章主要介紹了asp.net中IDataParameter調(diào)用存儲過程的實現(xiàn)方法,在asp.net數(shù)據(jù)庫程序設(shè)計中非常具有實用價值,需要的朋友可以參考下2014-09-09解析ASP.NET?Core中Options模式的使用及其源碼
這篇文章主要介紹了ASP.NET?Core中Options模式的使用及其源碼解析,在ASP.NET Core中引入了Options這一使用配置方式,其主要是為了解決依賴注入時需要傳遞指定數(shù)據(jù)問題(不是自行獲取,而是能集中配置),需要的朋友可以參考下2022-03-03