js-xlsx导出一个表,里面有多个sheet
时间: 2024-01-05 12:48:24 浏览: 139
要使用js-xlsx导出一个表,里面有多个sheet,你可以按照以下步骤操作:
1. 导入js-xlsx库。
```javascript
import XLSX from 'xlsx';
```
2. 定义数据。
```javascript
const data1 = [
['Sheet1', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
];
const data2 = [
['Sheet2', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
];
```
3. 创建workbook。
```javascript
const wb = XLSX.utils.book_new();
```
4. 将数据添加到sheet中。
```javascript
const ws1 = XLSX.utils.aoa_to_sheet(data1);
const ws2 = XLSX.utils.aoa_to_sheet(data2);
```
5. 将sheet添加到workbook中。
```javascript
XLSX.utils.book_append_sheet(wb, ws1, 'Sheet1');
XLSX.utils.book_append_sheet(wb, ws2, 'Sheet2');
```
6. 导出workbook。
```javascript
XLSX.writeFile(wb, 'file.xlsx');
```
这样就可以将多个sheet的数据导出到同一个Excel文件中。
阅读全文
相关推荐

















