zoukankan      html  css  js  c++  java
  • jsp+layui导出excel

    一、导入layui资源
    在页面写导出按钮
    <input type="hidden" id="excel" value="${time}">//获取数据的条件,可无
    <th id="exc" onclick="setExcel()" lang>excel</th>
    <script>
    function setExcel() {

    var time = document.getElementById("excel").value;//获取数据的参数,可无
    $.ajax({
    cache: false,
    type: "post",
    url: "${ctx}/Order/excel2.do",//获取数据后台接口
    data: "time=" + time,
    async: false,
    success: function (res) {
    var na = res.name;
    var tableStr = '<table border="0" cellspacing="" cellpadding="">'
    tableStr += '<tr style="font-size:16px;">';
    for (var i = 0; i < na.length; i++) {//动态添加表头
    tableStr += '<th width="15%">' + na[i] + '</th>';
    }
    tableStr += '</tr>';
    var len = res.list.length;
    var data = res;
    var count = 0;

    for (var i = 0; i < len; i++) {5
    tableStr += '<tr style="font-size:16px;">';
    tableStr += '<td>' + (i + 1) + '</td>';
    tableStr += '<td>' + res.list[i].roomNumber + '</td>';
    tableStr += '<td>' + res.list[i].PHP + '</td>';
    tableStr += '<td>' + res.list[i].RMB + '</td>';
    tableStr += '<td>' + res.list[i].rent + '</td>';
    tableStr += '<td>' + res.list[i].water + '</td>';
    tableStr += '<td>' + res.list[i].electricity + '</td>';
    /* tableStr += '<td>' + res.list[i].maintenanceCost + '</td>';*/
    tableStr += '<td>' + res.list[i].network + '</td>';
    tableStr += '<td>' + res.list[i].buildingManagementFee + '</td>';
    for (var d = 0; d < data.list[i].dlist.length; d++) {
    tableStr += '<td>' + data.list[i].dlist[d].mm + '</td>';
    }
    /* tableStr += '<td>' + res.list[i].linenCleaningfee + '</td>';
    tableStr += '<td>' + res.list[i].dailySupplies + '</td>';
    tableStr += '<td>' + res.list[i].otherExpenses + '</td>';*/
    tableStr += '<td>' + res.list[i].count + '</td>';
    tableStr += '</tr>';
    count++;
    }

    if (len == count) {
    tableStr += '<tr style="font-size:16px;">';
    tableStr += '<td>' + (len + 1) + '</td>';
    tableStr += '<td colspan="11">接单提成walkinguestdapfasom</td>';
    tableStr += '<td>' + res.booking + '</td>';
    tableStr += '</tr>';

    tableStr += '<tr style="font-size:16px;">';
    tableStr += '<td>' + (len + 2) + '</td>';
    tableStr += '<td colspan="11">合计Total(PHP)</td>';
    tableStr += '<td>' + res.sumPHP + '</td>';
    tableStr += '</tr>';

    tableStr += '<tr style="font-size:16px;">';
    tableStr += '<td>' + (len + 3) + '</td>';
    tableStr += '<td colspan="11">合计Total(CNY)</td>';
    tableStr += '<td>' + res.sumCNY + '</td>';
    tableStr += '</tr>';
    }
    tableStr += '</table>';

    exporExcel(time + "财务报表", tableStr);
    }
    });
    }

    /**
    * 注:如果想设置单元格格式,比如数字太多,默认导出会按科学计数法转换,这个时候要写成文本格式
    * 可以这样使用 在td 上 使用style;如:<td style='mso-number-format:"@";'>第一行 </td>
    * style='mso-number-format:"@";' 转文本
    * **/
    /**
    * @params: FileName:导出Excel的文件名称,excel:需要导出的table
    * 如果没有table列表,只有json数据的话,将json数据拼接成table字符串模板即可
    * **/
    function exporExcel(FileName, excel) {
    var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
    excelFile += '; charset=UTF-8">';
    excelFile += "<head>";
    excelFile += "<!--[if gte mso 9]>";
    excelFile += "<xml>";
    excelFile += "<x:ExcelWorkbook>";
    excelFile += "<x:ExcelWorksheets>";
    excelFile += "<x:ExcelWorksheet>";
    excelFile += "<x:Name>";
    excelFile += "{worksheet}";
    excelFile += "</x:Name>";
    excelFile += "<x:WorksheetOptions>";
    excelFile += "<x:DisplayGridlines/>";
    excelFile += "</x:WorksheetOptions>";
    excelFile += "</x:ExcelWorksheet>";
    excelFile += "</x:ExcelWorksheets>";
    excelFile += "</x:ExcelWorkbook>";
    excelFile += "</xml>";
    excelFile += "<![endif]-->";
    excelFile += "</head>";
    excelFile += "<body>";
    excelFile += excel;
    excelFile += "</body>";
    excelFile += "</html>";

    var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);

    var link = document.createElement("a");
    link.href = uri;

    link.style = "visibility:hidden";
    link.download = FileName; //格式默认为.xls

    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    }
    </script>
  • 相关阅读:
    windows系统切换jdk,修改java_home无效情况
    Cannot instantiate interface org.springframework.context.ApplicationListener
    MySQL分组查询获取每个学生前n条分数记录(分组查询前n条记录)
    ASP.NET Web API 使用Swagger生成在线帮助测试文档,支持多个GET
    EF TO MYSQL 无法查询中文的解决方法
    HttpWebRequest post请求获取webservice void数据信息
    This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分 解决方案
    MySQL 5.7.13解压版安装记录 mysql无法启动教程
    C# udpclient 发送数据断网后自动连接的方法
    汽车XX网站秒杀抢购代码
  • 原文地址:https://www.cnblogs.com/liuyuanchen/p/12697714.html
Copyright © 2011-2022 走看看