- 导出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;
}
- 导出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.复制文本
网友评论