前端导出excel文档代码
时间: 2023-09-03 16:08:54 浏览: 101
你可以使用以下代码来在前端导出Excel文档:
```javascript
function exportToExcel(data, filename) {
const csv = convertToCSV(data);
const csvData = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
if (link.download !== undefined) {
const url = URL.createObjectURL(csvData);
link.setAttribute('href', url);
link.setAttribute('download', filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
function convertToCSV(data) {
const rows = [];
for (let i = 0; i < data.length; i++) {
const row = [];
for (let j = 0; j < data[i].length; j++) {
row.push('"' + data[i][j] + '"');
}
rows.push(row.join(','));
}
return rows.join('\n');
}
```
你可以调用 `exportToExcel` 函数来导出一个二维数组 `data` 到一个名为 `filename` 的Excel文件中。请确保你的浏览器支持下载功能,以及传入的数据是正确格式的二维数组。
阅读全文
相关推荐














