zoukankan      html  css  js  c++  java
  • MVC 导出Excel 的其中一方法(View导出excel)

    场景: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>

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

  • 相关阅读:
    NOI2017 退役记
    bzoj2590[Usaco2012 Feb]Cow Coupons
    bzoj2215[POI2011]Conspiracy
    bzoj2115[WC2011]Xor
    bzoj4754[JSOI2016]独特的树叶
    SQL外连接与条件 left outer join + WHERE/AND 区别
    IBM Datastage
    无法在MS SQL SERVER MANAGEMENT中打开SSIS实例-解决办法
    SSIS 教学教程
    SQL SERVER 2019 电子书下载
  • 原文地址:https://www.cnblogs.com/Fyhong/p/3608522.html
Copyright © 2011-2022 走看看