Skip to content

Too few data or empty worksheet generate malformed excel file #99

Closed
@bluedge

Description

@bluedge

In streaming mode I came across this bug. If you add only one row to a worksheet OR add a worksheet that you do not fill the Excel file will be malformed. See the exemple below, uncomment the second line of push() to see the bug vanish.
Note: removing the worksheet columns header produce the same bug.

var stream      = require('stream');
var util            = require('util');
var Excel           = require('exceljs');
var fs          = require('fs');

var writableStream  = fs.createWriteStream('./streamed1.xlsx');

var ExcelTransform = function(options) {
    stream.Transform.call(this, {
        writableObjectMode: true,
        readableObjectMode: false
    });
        this.workbook = options.workbook;
    var self = this;

    this.workbook.stream.on('readable', function() {
        var chunk = workbook.stream.read();
        self.push(chunk);
    });
    this.worksheet = options.worksheet;
}
util.inherits(ExcelTransform, stream.Transform);

ExcelTransform.prototype._transform = function(doc, encoding, callback) {
    this.worksheet.addRow({
        name: doc.name
    }).commit();
    callback();
};

ExcelTransform.prototype._flush = function(callback) {
    this.workbook.commit(); // final commit
};

// it's better to provide the workbook as a parameter to the ExcelTransform
var workbook = new Excel.stream.xlsx.WorkbookWriter();
var worksheet = workbook.addWorksheet('sheet 1');
worksheet.columns = [{
    header: 'Name',
    key: 'name'
}];

var rs = new stream.Readable({ objectMode: true });
rs.push({ name: 'one' });
// rs.push({ name: 'two' }); // uncomment me to see the bug vanish
rs.push(null);

rs.pipe(new ExcelTransform({
    workbook: workbook,
    worksheet: worksheet
})).pipe(writableStream);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions