javascript - How to style xlsx files in node.js - Stack Overflow

I'm exporting a file generated by xlsx.js with my node.js express application server. I'd lik

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?

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?

Share Improve this question asked Jan 24, 2014 at 14:23 Fra HFra H 3451 gold badge5 silver badges20 bronze badges 2
  • Not sure if there is an order of operations but in his test case it looks like he puts bold before the value. Also try single quotes...I'm just throwing ideas out there I've never used this before. Also sweet name. – Bryce Easley Commented Jan 24, 2014 at 14:44
  • @Thunda lol, yours too. Just tried that.. unfortunately without success. Ty anyway – Fra H Commented Jan 24, 2014 at 14:49
Add a ment  | 

2 Answers 2

Reset to default 2

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."
}));
transporter.sendMail({
    from: "abc.gmail.",
    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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744186789a4562241.html

相关推荐

  • javascript - How to style xlsx files in node.js - Stack Overflow

    I'm exporting a file generated by xlsx.js with my node.js express application server. I'd lik

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信