xb18
xb18
文章78
标签0
分类0
xlsx的使用

xlsx的使用

导出excel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import * as XLSX from 'xlsx';

const fileName = 'test';
let data = [
[
ctx.__('test1'),
ctx.__('test2'),
ctx.__('test3')
]
];
const workbook = XLSX.utils.book_new();
const worksheet = XLSX.utils.json_to_sheet(data, { skipHeader: true });
XLSX.utils.book_append_sheet(workbook, worksheet, 'sheetName');
// eggjs导出excel
this.ctx.set('Content-Type', 'application/vnd.openxmlformats');
this.ctx.set(
'Content-Disposition',
"attachment;filename*=UTF-8' '" + encodeURIComponent(fileName) + '.xlsx',
);
this.ctx.body = await XLSX.write(workbook, {
bookType: 'xlsx',
type: 'buffer',
});

设置单元格格式

使用xlsx-style

1
2
3
4
5
6
7
8
9
10
11
12
13
import xlsxStyle from '`xlsx-style`';
import * as XLSX from 'xlsx';

/**使用XLSX导出excel的workbook**/
const workbook = XLSX.utils.book_new();
const worksheet = XLSX.utils.json_to_sheet([['test']], { skipHeader: true });
XLSX.utils.book_append_sheet(workbook, worksheet, 'sheetName');

// 使用xlsxStyle写出excel
this.ctx.body = await xlsxStyle.write(workbook, {
bookType: 'xlsx',
type: 'buffer',
});
本文作者:xb18
本文链接:https://moelj.com/2024/01/27/xlsx%E4%BD%BF%E7%94%A8/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可