您的位置:首页 > 其它

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

2016-01-05 21:13 246 查看
近来接手的项目,有几个很值得分享的东西。经过自己的不懈实践,总结,分享给大家,希望能对大家的学习有点帮助。

本次探讨的是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" onkeydown="Enter();" maxlength="20" style="width:150px;">
<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="stuSearch()" >查询</a>
<a id="download" @*href="/FreshStudent/ExportExcel"*@ class="easyui-linkbutton" style="margin-left :720px";data-options="iconCls:'',plain:true" onclick="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)!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: