nodejs:表格数据导出为excel

 安装依赖

npm install node-excel-export

server/ app.js

const excel = require('node-excel-export');
app.get('/api/getexcel', async (req, res) => {
    let dataset = await queryAllPlData(req.query) // 这里不做详情介绍,就是通过sql拿到正常的表格数据,具体代码可以参考我的另一篇文章
    // 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);
})

excel.vue

//导出
getexport () {
	const { account, name, info } = this.filters // 参数
	let url = `api/getexcel?account=${account}&name=${name}&info=${info}`
    window.location = url;//这里不能使用get方法跳转,否则下载不成功
},

----------------------------------

有问题欢迎留言~

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值