nodejs 导出excel

            window.export = function() {
                layer.msg(MOD_PAGE_PATH + '/export');
                window.location = MOD_PAGE_PATH + '/export'
            }
const excel = require('node-excel-export');


export: async (req, res) => {
		let dataset = [
			{id:1,sex:1,remark:'sss'},
			{id:2,sex:0,remark:'ddd'},
			{id:3,sex:1,remark:'ggg'},
		]
		
        // excel样式
        const styles = {
            headerNormal: {
                fill: {
                    fgColor: {
                        rgb: 'dd4b39'
                    }
                },
                border: {
                    top: { style: 'thin', color: '000000' },
                    bottom: { style: 'thin', color: '000000' },
                    left: { style: 'thin', color: '000000' },
                    right: { style: 'thin', color: '000000' }
                },
                font: {
                    bold: true,
                    sz: 11,
                    color: {
                        rgb: 'ffffff'
                    }
                }
            },
            cellNormal: {
                fill: {
                    fgColor: {
                        rgb: 'FFFFFF'
                    }
                },
                border: {
                    top: { style: 'thin', color: '000000' },
                    bottom: { style: 'thin', color: '000000' },
                    left: { style: 'thin', color: '000000' },
                    right: { style: 'thin', color: '000000' }
                },
                font: {
                    sz: 10
                }
            },
            cellRed: {
                // fill: {
                //     fgColor: {
                //         rgb: 'ff0000'
                //     }
                // }
                border: {
                    top: { style: 'thin', color: '000000' },
                    bottom: { style: 'thin', color: '000000' },
                    left: { style: 'thin', color: '000000' },
                    right: { style: 'thin', color: '000000' }
                },
                font: {
                    sz: 10,
                    color: {
                        rgb: 'ff0000'
                    }
                }
            },
        };
		// excel列表头数据
        const specification = {
            id: {
                displayName: 'id',
                headerStyle: styles.headerNormal,
                cellStyle: styles.cellNormal,
                width: '13'
            },
            sex: {
                displayName: '性别',
                headerStyle: styles.headerNormal,
                cellFormat: function(value, row) {
                    return (value == 1) ? '男' : '女';
                },
                cellStyle: function(value, row) {
                    return (row.sex== 1) ? styles.cellRed : styles.cellNormal;
                },
                width: '12'
            },
            remark: {
                displayName: '备注',
                headerStyle: styles.headerNormal,
                cellFormat: function(value, row) {
                    return value ? value : '-';
                },
                cellStyle: function(value, row) {
                    return (row.remark_bgground== 'red') ? styles.cellRed : styles.cellNormal;
                },
                width: '16'
            },
        }
		// excel配置
        const report = excel.buildExport(
            [ 
                {
                    name: 'Report', // <- Specify sheet name (optional)
                    // heading: heading, // <- Raw heading array (optional)
                    // merges: merges, // <- Merge cell ranges
                    specification: specification, // <- Report specification
                    data: dataset // <-- Report data
                }
            ]
		);
		
        res.attachment('下载表.xlsx');
        res.send(report);
	},

posted @ 2023-01-18 20:58  盘思动  阅读(448)  评论(0编辑  收藏  举报