日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【项目总结】之——导出Excel

發布時間:2025/5/22 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【项目总结】之——导出Excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

近來接手的項目,有幾個很值得分享的東西。經過自己的不懈實踐,總結,分享給大家,希望能對大家的學習有點幫助。

本次探討的是mvc框架之中的一種導出方法,導出excel。

先讓大家看一下啊我們的view界面:


我們的view代碼:

<body><div style="margin-top:30px;margin-left:20px;"><div class="easyui-panel" title="查詢" style="width:1240px;"><div style="margin:10px 20px;float:left">@*加載搜索框*@請輸入要查詢的內容:<input class="easyui-textbox" id="txtSearch" name="txtSearch" οnkeydοwn="Enter();" maxlength="20" style="width:150px;"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" οnclick="stuSearch()" >查詢</a><a id="download" @*href="/FreshStudent/ExportExcel"*@ class="easyui-linkbutton" style="margin-left :720px";data-options="iconCls:'',plain:true" οnclick="Export()">導出Excel</a> </div></div></div><div id="ContentAreas" style="margin-top: 15px; margin-left: 20px; width: 960px;"><table id="dt" class="easyui-datagrid" title="學生報表" style="width: 1240px; height: auto"data-options="url:'/FreshStudent/SelectStu',singleSelect:true,pagination:true"><thead><tr><th data-options="field:'StudentCode',width:150,align:'center',sortable:true" >學號</th><th data-options="field:'Name',width:120,align:'center'">姓名</th><th data-options="field:'strSex',width:120,align:'center'">性別</th><th data-options="field:'Score',width:120,align:'center'">分數</th><th data-options="field:'Level',width:120,align:'center'">學歷</th><th data-options="field:'FreshMajorName',width:150,align:'center'">專業名稱</th><th data-options="field:'FreshDepartmentName',width:150,align:'center'">學院名稱</th><th data-options="field:'TelNum',width:150,align:'center'">聯系方式</th><th data-options="field:'strCheckIn',width:120,align:'center'">是否報道</th></tr></thead></table></div></body>



controller代碼分享:


<span style="font-size:18px;">#region 導出數據Excel --李衛中--2016年1月5日21:00:10/// <summary>/// 導出數據/// </summary>/// <param name="strlike"></param>/// <returns></returns>public ActionResult ExportExcel(string strlike) {//獲取前臺界面容量int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);//獲取前臺界面當前頁int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);//定義總數,接收后臺記錄數目int total;List<FreshStudentViewModel> stuList = new List<FreshStudentViewModel>();//執行模糊查詢if (strlike == "" || strlike == null){stuList = stuService.ExportAllStu();}else { stuList = stuService.ExportStu(strlike); } //給Excel導出里面添加表頭Hashtable headnames = new Hashtable();headnames.Add("Score", "分數");headnames.Add("Level", "學歷");headnames.Add("FreshMajorName", "專業名稱");headnames.Add("StudentCode", "學號");headnames.Add("Name", "姓名");headnames.Add("strSex", "性別");headnames.Add("FreshDepartmentName", "學院名稱");headnames.Add("TelNum", "聯系方式");headnames.Add("strCheckIn", "是否報道");//導出新生報表信息return File(ExportManager.ExportExcel(stuList, headnames), "application/vnd.ms-excel", "新生報表.xls");}#endregion</span>



Js方法分享:

<span style="font-size:18px;">function Export() {var strlike = $("#txtSearch").val();window.location.href = "/FreshStudent/ExportExcel?strlike=" + strlike;}</span>
然后,我們的后臺方法就不再累述了,就是從數據庫中查詢得到的數據集合,返回之后有我們的controller來接收。


效果展示:



遺留問題:導出的數據表頭順序發生錯亂,請關注本篇博客后續解決方案!

小結:

這個方法其實是很簡單的,但是其中有一個地方難住了我大約有一個小時,就是通過點擊我們用controller來獲取id="txtSearch"的值,這個在很多人開來很簡單,其實對我來說也是很簡單,等我寫出這個方法后發現,總結。自己動手的機會還是太少了,以至于有些地方想不到,我們有句話叫“不怕不知道,就怕不知道”,我們不是不會,是總也想不到。這比我們想到了不會要嚴重得多。所以我們要把握好每一次我們遇到困難的機會,每一個機會都會帶給我們一點收獲,一些改變。

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?加油! ?少(sao)年(nian)!



轉載于:https://www.cnblogs.com/DoubleEggs/p/5747167.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的【项目总结】之——导出Excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。