vue---导出CSV
对于前端导出数据,如果导出excel需要用一些第三方插件,还要安装插件,但是如果导出csv就很简单了。
代码示例:
/** * arrayTocsv * arrays = [[张三]] * headers = ['姓名'] */ export function arrayTocsv(arrays,headers,filename){ const ctime = new Date(); const fillzero = function(num){ return num < 10 ? ('0'+num) : num; } const suffix = ctime.getFullYear() + '' + (fillzero(ctime.getMonth() + 1)) + '' + fillzero(ctime.getDate()) + '' + fillzero(ctime.getHours()) + '' + fillzero(ctime.getMinutes()) + '' + fillzero(ctime.getSeconds()); const filenames = filename ? (filename+'-'+suffix) : ('export' + suffix); let csvContent = headers ? headers.join(',') + '\r\n' : ''; arrays.forEach(row => { csvContent += row.join(',') + '\r\n'; }); const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); const link = document.createElement('a'); const url = URL.createObjectURL(blob); link.href = url; link.setAttribute('download', filenames); document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); }
打完收工!

浙公网安备 33010602011771号