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 @   盘思动  阅读(465)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2022-01-18 变更单元格背景颜色
2019-01-18 微信小程序退款 处理类
2018-01-18 getQueryString----获取url中得参数
点击右上角即可分享
微信分享提示