日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

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

本次探討的是mvc框架之中的一種導(dǎo)出方法,導(dǎo)出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">@*加載搜索框*@請輸入要查詢的內(nèi)容:<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()">導(dǎo)出Excel</a> </div></div></div><div id="ContentAreas" style="margin-top: 15px; margin-left: 20px; width: 960px;"><table id="dt" class="easyui-datagrid" title="學(xué)生報表" 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" >學(xué)號</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'">分?jǐn)?shù)</th><th data-options="field:'Level',width:120,align:'center'">學(xué)歷</th><th data-options="field:'FreshMajorName',width:150,align:'center'">專業(yè)名稱</th><th data-options="field:'FreshDepartmentName',width:150,align:'center'">學(xué)院名稱</th><th data-options="field:'TelNum',width:150,align:'center'">聯(lián)系方式</th><th data-options="field:'strCheckIn',width:120,align:'center'">是否報道</th></tr></thead></table></div></body>



controller代碼分享:


<span style="font-size:18px;">#region 導(dǎo)出數(shù)據(jù)Excel --李衛(wèi)中--2016年1月5日21:00:10/// <summary>/// 導(dǎo)出數(shù)據(jù)/// </summary>/// <param name="strlike"></param>/// <returns></returns>public ActionResult ExportExcel(string strlike) {//獲取前臺界面容量int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);//獲取前臺界面當(dāng)前頁int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);//定義總數(shù),接收后臺記錄數(shù)目int total;List<FreshStudentViewModel> stuList = new List<FreshStudentViewModel>();//執(zhí)行模糊查詢if (strlike == "" || strlike == null){stuList = stuService.ExportAllStu();}else { stuList = stuService.ExportStu(strlike); } //給Excel導(dǎo)出里面添加表頭Hashtable headnames = new Hashtable();headnames.Add("Score", "分?jǐn)?shù)");headnames.Add("Level", "學(xué)歷");headnames.Add("FreshMajorName", "專業(yè)名稱");headnames.Add("StudentCode", "學(xué)號");headnames.Add("Name", "姓名");headnames.Add("strSex", "性別");headnames.Add("FreshDepartmentName", "學(xué)院名稱");headnames.Add("TelNum", "聯(lián)系方式");headnames.Add("strCheckIn", "是否報道");//導(dǎo)出新生報表信息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>
然后,我們的后臺方法就不再累述了,就是從數(shù)據(jù)庫中查詢得到的數(shù)據(jù)集合,返回之后有我們的controller來接收。


效果展示:



遺留問題:導(dǎo)出的數(shù)據(jù)表頭順序發(fā)生錯亂,請關(guān)注本篇博客后續(xù)解決方案!

小結(jié):

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

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



轉(zhuǎn)載于:https://www.cnblogs.com/DoubleEggs/p/5747167.html

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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。