P
P
Pix32019-01-25 07:04:01
Node.js
Pix3, 2019-01-25 07:04:01

How to export data to excel from database?

I am using the excel4node library .
I want to export data to excel from a database. So far, I'm only trying to do this by exporting certain data by date. In general, when exporting, Excel is created, but there is not a single record.

var xl = require('excel4node')
const db = require('../config/db.config.js')
const Report = db.report

var wb = new xl.Workbook()


var ws = wb.addWorksheet('Отчет');

var style = wb.createStyle({
    font: {
        color: '#000000',
        size: 12
    }
});

module.exports.getexceldata = async function(req, res) {
    try {
         await Report.findAll({
            where: {
                report_date: req.params.report_date
            }
        })
        ws.cell(1,1).string({name: req.body.name}).style(style)
        wb.write('Excel.xlsx');
        res.download('\Excel.xlsx');
    } catch(e) {
        errorHandler(res, e)
    }
}

What should this line look like correctly?
ws.cell(1,1).string({name: req.body.name}).style(style)

Just in case, I will explain that this is a reference to the model:
const db = require('../config/db.config.js')
const Report = db.report

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2019-01-25
@Iv_and_S

ws.cell(1,1).string({name: req.body.name}).style(style)

what kind of data is there, what type? exactly a string?
I would recommend to look in the debugger and prepare the data before recording.
try to write down a line at first. If everything is OK, then you have prepared the data incorrectly.
those.
var dataForWrite = Str(результат запроса);
ws.cell(1,1).string(dataForWrite).style(style);

the library is definitely working, at least in terms of data recording.
I used it here, but the examples are scattered throughout the code. - it's not clear what and why in the example.
res.download('\Excel.xlsx');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question