MVC 导出Excel 的其中一方法(View导出excel)

时间:2022-06-02 08:51:13

场景: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);
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }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>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

结论:简单的导出,大数据下请自己测试.