nodejs 创建并输出xlsx

install xlsx

1
npm install --save xlsx

demo

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';

export default async (req, res) => {
const { type = 'xlsx', name = 'export' } = req.query;

const Workbook = xlsx.utils.book_new();
let wsData = [
{ ヘッダ1: 1, ヘッダ2: 2 },
{ ヘッダ1: 'A', ヘッダ2: 'B' }
];
let option = {
header: ['ヘッダ1', 'ヘッダ2']
};

let ws = xlsx.utils.json_to_sheet(wsData, option);

xlsx.utils.book_append_sheet(Workbook, ws, 'test1');
xlsx.utils.book_append_sheet(Workbook, ws, 'test2');

const data = xlsx.write(Workbook, { type: 'buffer', bookType: type });
res.attachment(`${name}.${type}`);
res.status(200).send(data);
};
#

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×