سؤال

I'm exporting a file generated by xlsx.js with my node.js express application server. I'd like to know how to put bold in first line, set auto width and (eventually) colour borders. For example, the first line of the worksheet looks like:

var data = [[
        {value:"EXTREMELY LONG TITLE 1", bold: 1, autoWidth:true},
        {value:"TITLE2"},
        {value:"TITLE3"}
    ]];

My actual result is that these three cells are correctly printed and served. The attributes bold and autoWidth are ignored. How can I get this to work?

هل كانت مفيدة؟

المحلول

There are a lot of modules that can do it. But if you want to control the formatting of xlsx file, then I suggest you use this below code. Rows contain data in the form of JSON array.

var excel = require('node-excel-export');
var styles = {
    headerDark: {
        fill: {
            fgColor: {
                rgb: 'FF000000'
            }
        },
        font: {
            color: {
                rgb: 'FFFFFFFF'
            },
            sz: 14,
            bold: true,
            underline: true
        }
    },
    cellPink: {
        fill: {
            fgColor: {
                rgb: 'FFFFCCFF'
            }
        }
    },
    cellGreen: {
        fill: {
            fgColor: {
                rgb: 'FF00FF00'
            }
        }
    }
};

var specification = {
    "Col1": {
        "displayName": 'Col1Name',
        "headerStyle": styles.headerDark,
        "width": 250
    },
    "Col2": {
        "displayName": 'Col2Name',
        "headerStyle": styles.headerDark,
        "width": 215
    },
    "Col3": {
        displayName: 'Col3Name',
        headerStyle: styles.headerDark,
        width: 150
    }
}

var report = excel.buildExport(
    [{
        name: 'Report.xlsx',
        specification: specification,
        data: rows
    }]
);

This can then be sent either as response in the API :

res.setHeader('Content-disposition', 'attachment; filename=Report.xlsx');
res.send(report);

Or as an attachment in email:

var smtpTransport = require('nodemailer-smtp-transport');
var transporter = nodemailer.createTransport(smtpTransport({
    host: "mail.yourmailman.com"
}));
transporter.sendMail({
    from: "abc.gmail.com",
    to: toAddr,
    subject: 'scheduled reporting',
    attachments: [{
        'filename': 'Report.xlsx',
        'content': new Buffer(report, 'base64')
    }]
}, function(error, success) {
    if (error) {
        logger.error('Error wile sending mail ', error);
        return;
    } else {
        logger.info('Mail sent.. ', success);
        return;
    }
});

نصائح أخرى

You could use xlsx, js-xlsx or node-xlsx. Create a formatted template and then insert the data into it.

This may help

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top