场景:mvc下导出excel
思路:使用View导出excel
步骤:
1.导出标签添加事件
$("#export_A").click(function(){
//省略代码.....
window.location.href = "/DuplicateTaskManager/DuplicateTaskManager/ExportExcel/?taskId=" + taskId + "&unitId=" + unitId + "&levelId=" + levelId;
});
2.准备View
public ActionResult ExportExcel(int taskId, int unitId, int levelId) {
HttpContext.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Response.Charset = "utf-8"; HttpContext.Response.ContentEncoding = Encoding.UTF8; HttpContext.Response.AppendHeader("Content-Disposition", "attachment; filename="" + UTF_FileName("审核结果查询_" + DateTime.Now.ToString("yyyyMMddHHmmss")) + ".xls""); //list是需要下载出的数据,和导出设置无关,正常获取数据,在View遍历
return PartialView(list); } /// <summary> /// 将文件名转为UrlEncode /// </summary> /// <param name="filename"></param> /// <returns></returns> private static string UTF_FileName(string filename) { return HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8); }
3.View 遍历数据
<table border="1"> <thead> <tr> <td width="100" rowspan="2"> 单位 </td> <td width="60" rowspan="2"> 审核总数 </td> <td rowspan="2"> 未审核数 </td> <td colspan="<%=ss.Keys.Count%>"> <span style="margin-left: 40%;">已审核数</span> </td> <td width="60" rowspan="2"> 已完成审核比率 </td> <td rowspan="2"> 排名 </td> </tr> <tr> <% foreach (var item in ss.Keys) { %> <td width="100"> <%=ss.Get(item.ToString()) %> </td> <% } %> </tr> <% int i = 1; foreach (var item in Model) {%> <tr> <td> <%=item.UnItName %> </td> <td> <%=item.SumCount %> </td> <td> <%=item.NotApprovedCount %> </td> <% foreach (var key in ss.Keys) { %> <td> <%=item.Dcits.Get(key.ToString()) %> </td> <% } %> <td> <%=string.Format("{0}%", item.CompleteRate.ToString("0.00"))%> </td> <td> <% if (i < Model.Count) {%> <%=i%> <%} %> </td> </tr> <% i++; }%> </thead> </table>
结论:简单的导出,大数据下请自己测试.