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

