網站首頁 編程語言 正文
有關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")
原文鏈接:https://www.cnblogs.com/darrenji/p/3787771.html
相關推薦
- 2022-09-09 python實現0到1之間的隨機數方式_python
- 2023-10-15 #css# 超出高度,可上下滾動
- 2022-05-08 Windows?Bat腳本實現定時重啟應用程序的項目實踐_DOS/BAT
- 2023-07-08 echarts多個series進行自定義tooltip的數據顯示
- 2023-10-15 理解C/C++中的鏈接
- 2023-02-12 Android?onbackpressed實現返回鍵的攔截和彈窗流程分析_Android
- 2022-06-29 python人工智能tensorflow函數tensorboard使用方法_python
- 2022-02-27 Error in render: “TypeError: Cannot read propertie
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支