vue2中导出excel
时间: 2024-06-25 15:00:56 浏览: 155
在 Vue.js 2.x 中,导出 Excel 数据通常会使用第三方库,如 `js-xlsx` 或者 `xlsx-writer`。以下是使用 `js-xlsx` 库导出 Excel 的基本步骤:
1. 安装库:
```bash
npm install js-xlsx --save
```
2. 在 Vue 组件中引入并使用:
```html
<script>
import XLSX from 'js-xlsx';
</script>
```
3. 假设你有一个包含数据的数组 `dataArray`,在你需要导出的地方(例如点击事件处理函数):
```javascript
exportExcel() {
const workbook = XLSX.utils.book_new();
const worksheet = XLSX.utils.json_to_sheet(dataArray);
// 添加表头
const headerRow = [yourHeaderArray];
XLSX.utils.sheet_add_aoa(worksheet, [headerRow]);
// 写入文件
const filename = 'report.xlsx';
const type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; // xlsx 格式
const data = XLSX.writeFile(workbook, filename, { type });
// 下载文件
const link = document.createElement('a');
link.href = 'data:' + type + ';base64,' + window.btoa(data);
link.download = filename;
link.click();
}
```
请替换 `yourHeaderArray` 为你的实际列名数组。这个函数会在用户的浏览器中下载一个名为 `report.xlsx` 的 Excel 文件。
阅读全文
相关推荐
















