網(wǎng)站首頁 編程語言 正文
有關(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強類型集合視圖:
@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")
原文鏈接:https://www.cnblogs.com/darrenji/p/3787771.html
相關(guān)推薦
- 2022-08-10 go?modules中replace使用方法_Golang
- 2022-07-29 使用React?Router?v6?添加身份驗證的方法_React
- 2022-11-03 Python列表推導(dǎo)式,元組推導(dǎo)式,字典推導(dǎo)式,集合推導(dǎo)式_python
- 2022-03-24 C語言常見的文件操作函數(shù)_C 語言
- 2022-08-27 asp.net中MVC的處理流程詳解_基礎(chǔ)應(yīng)用
- 2022-07-02 ASP.NET配置文件中自定義節(jié)點_實用技巧
- 2022-11-13 Git如何實現(xiàn)checkout遠(yuǎn)程tag_相關(guān)技巧
- 2022-09-27 Android內(nèi)置的OkHttp用法介紹_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支