简体   繁体   English

如何使用node-excel-export使用公式导出Excel工作表?

[英]How to export excel sheet with formula using node-excel-export?

I am using node-excel-export module in node.js to export my data from DB into excel sheet. 我正在使用node.js中的node-excel-export模块将数据从数据库导出到excel工作表中。

The problem here is that, i want the excel sheet to be shipped with formulae applied on specific cells. 这里的问题是,我希望Excel表格随特定单元格上应用的公式一起发货。

The documentation in the site does not demonstrate the usage of formulae clearly. 该站点中的文档没有清楚地说明公式的用法。

This is what i have tried: 这是我尝试过的:

var XLSX = require('xlsx');

function getSheet(data, opts) {
    var ws = {};

    var range = {
        s: {
            c:0, 
            r:0
        }, 
        e: {
            c:3, 
            r:3 
        }
    };

    for(var R = 0; R < 4; R++) {
        for(var C = 0; C < 3; C++) {

            var cell = {
                v: data[R][C] ,
                t: 'n'
            };

            var cell_ref = XLSX.utils.encode_cell({
                c:C,
                r:R
            });

            if(C == 2) {
                cell_ref.f = "A"+R+"B"+R;
            }

            ws[cell_ref] = cell;
        }
    }
    ws['!ref'] = XLSX.utils.encode_range(range);
    return ws;
}


var data = [
    [1, 2],
    [3, 4],
    [5, 6], 
    [7, 8]
];

function Workbook() {
    if(!(this instanceof Workbook)) {
        return new Workbook();
    }
    this.SheetNames = [];
    this.Sheets = {};
}

var wb = new Workbook();
var wsName = "SheetJS";
wb.SheetNames.push(wsName);
wb.Sheets[wsName] = getSheet(data);


module.exports = {
    dataset: [],
    getExcelFile: function (resultset, res) {

        /* write file */
        XLSX.writeFile(wb, 'test.xlsx');

        res.download('test.xlsx');
    }
};

` `

please help. 请帮忙。

OK solution found. 确定解决方案。

I did two mistakes 我犯了两个错误

1.Instead of cell_ref.f use cell.f . 的1.Instead cell_ref.f使用cell.f

2.Formula correction. 2.配方校正。

Updated code if it helps others: 更新了代码,如果它可以帮助其他人:

var XLSX = require('xlsx');

function getSheet(data, opts) {
    var ws = {};

    var range = {
        s: {
            c:0, 
            r:0
        }, 
        e: {
            c:3, 
            r:3 
        }
    };

    for(var R = 0; R < 4; R++) {
        for(var C = 0; C < 3; C++) {

            var cell = {
                v: data[R][C],
                t: 'n'
            };

            var cell_ref = XLSX.utils.encode_cell({
                c:C,
                r:R
            });

            if(C == 2) {
                cell.f = "A"+ (R+1) +"+B" + (R+1);
            }

            ws[cell_ref] = cell;
        }
    }
    ws['!ref'] = XLSX.utils.encode_range(range);
    return ws;
}


var data = [
    [1, 2],
    [3, 4],
    [5, 6], 
    [7, 8]
];

function Workbook() {
    if(!(this instanceof Workbook)) {
        return new Workbook();
    }
    this.SheetNames = [];
    this.Sheets = {};
}

var wb = new Workbook();
var wsName = "SheetJS";
wb.SheetNames.push(wsName);
wb.Sheets[wsName] = getSheet(data);


module.exports = {
    dataset: [],
    getExcelFile: function (resultset, res) {

        /* write file */
        XLSX.writeFile(wb, 'test.xlsx');

        res.download('test.xlsx');
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM