vue中导出excel怎么设置
时间: 2024-05-02 08:21:54 浏览: 91
要在Vue中导出Excel,可以使用第三方库,如`xlsx`或`file-saver`。
使用`xlsx`库:
1. 安装`xlsx`库:
```bash
npm install xlsx --save
```
2. 在组件中导入`xlsx`:
```js
import XLSX from 'xlsx'
```
3. 创建一个Excel文件:
```js
const worksheet = XLSX.utils.aoa_to_sheet([
['姓名', '年龄'],
['张三', 18],
['李四', 20]
])
const workbook = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1')
```
4. 将文件下载到本地:
```js
const wbout = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' })
const blob = new Blob([wbout], { type: 'application/octet-stream' })
saveAs(blob, 'example.xlsx')
```
使用`file-saver`库:
1. 安装`file-saver`库:
```bash
npm install file-saver --save
```
2. 在组件中导入`file-saver`:
```js
import { saveAs } from 'file-saver'
```
3. 创建一个Excel文件:
```js
const data = [['姓名', '年龄'], ['张三', 18], ['李四', 20]]
const ws = XLSX.utils.aoa_to_sheet(data)
const wb = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')
```
4. 将文件下载到本地:
```js
const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'array' })
const blob = new Blob([wbout], { type: 'application/octet-stream' })
saveAs(blob, 'example.xlsx')
```
阅读全文