zoukankan      html  css  js  c++  java
  • js导出不规则表格/form类表格

    window.tableToExcel = (function() {
        var uri = 'data:application/vnd.ms-excel;base64,'
            , template = '<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"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->'+
        ' <style type="text/css">'+
        '.excelTable  {'+
        'border-collapse:collapse;'+
        ' border:thin solid #999; '+
        '}'+
        '   .excelTable  th {'+
        '   border: thin solid #999;'+
        '  padding:20px;'+
        '  text-align: center;'+
        '  border-top: thin solid #999;'+
        ' '+
        '  }'+
        ' .excelTable  td{'+
        ' border:thin solid #999;'+
        '  padding:2px 5px;'+
        '  text-align: center;'+
        ' }</style>'+'</head><body><table border="1">{table}</table></body></html>'
            , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
            , format = function(s, c) { return s.replace(/{(w+)}/g, function(m, p) { return c[p]; }) }
        return function(table, name) {
            if (!table.nodeType) table = document.getElementById(table);
            var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
            var downloadLink = document.createElement("a");
            downloadLink.href = uri + base64(format(template, ctx));
            downloadLink.download = name+".xls";
            document.body.appendChild(downloadLink);
            downloadLink.click();
            document.body.removeChild(downloadLink);
        }
    })()
    

      

  • 相关阅读:
    webpack介绍 安装 常用命令
    npm常用命令
    jQuery 实现点击页面其他地方隐藏菜单
    :after伪类+content内容生成经典应用举例
    几分钟前 几小时前 几天前... 7个阶段
    JS — 获取4个不重复的随机验证码
    JS — 数组去重(4种方法)
    Spring IOC原理简析
    浅谈Linux下的五种I/O模型
    大型分布式网站架构技术总结
  • 原文地址:https://www.cnblogs.com/zhangtianqi520/p/11451134.html
Copyright © 2011-2022 走看看