美文网首页
导出函数

导出函数

作者: 励志摆脱懒癌的少女酱 | 来源:发表于2018-08-15 09:36 被阅读9次
    1. 导出CSS文件
    function loadCSS(src){
        var d = $.Deferred();
        var link = document.createElement('link');
        link.type = 'text/css';
        link.rel = 'stylesheet';
        link.onload = function() {
            link.onload = null;
            d.resolve(link.href);
        }
        link.href = src;
        document.head.appendChild(link);
    
        return d;
    }
    
    1. 导出CSV
    function exportCSV(data, headers, fileName, otherData){
            let other = '';
        if (otherData) {
            otherData.forEach(item => {
                other += item.name + ',' + item.val + '\n';
            });
            other += '\n';
        }
        headers = other + headers.join(',') + '\n';
        let content = '';
        data.forEach(item => {
            let keys = Object.keys(item);
            keys.forEach((key, index) => {
                let val = item[key];
                            // 以防字段出现","时导致下载格式错乱
                if(String(val).indexOf(',') != -1){
                    val = '"' + val + '"';
                              }
                          // 每列用","隔开;
                content += val + (index != keys.length - 1 ? ',' : '');
            });
                  // 每行用'\n'隔开
            content += '\n';
        });
        let a = document.createElement('a');
        let str = encodeURIComponent(headers + content); // 对"\n"进行转义
        a.href = 'data:text/csv;charset=utf-8,\ufeff' + str;  // \ufeff解决中文乱码
        a.download = download;
        document.body.appendChild(a);
        a.click();
            document.body.removeChild(a);
    }
    

    3.复制文本

    相关文章

      网友评论

          本文标题:导出函数

          本文链接:https://www.haomeiwen.com/subject/hrsibftx.html