Modify existing Excel File using node.js

与世无争的帅哥 提交于 2019-12-18 19:05:57

问题


Is there any way to modify existing excel file in node.js? I've looked into exceljs but it does not provide any functionality that will just modify the existing data. It seems like it will write to a new stream.

Or did I miss something on exceljs?

Thanks in advance.


回答1:


exceljs does let you modify Excel spreadsheets.

Here's an example of reading in an existing spreadsheet and writing it back out to a different file:

var Excel = require('exceljs');
var workbook = new Excel.Workbook();

workbook.xlsx.readFile('old.xlsx')
    .then(function() {
        var worksheet = workbook.getWorksheet(1);
        var row = worksheet.getRow(5);
        row.getCell(1).value = 5; // A5's value set to 5
        row.commit();
        return workbook.xlsx.writeFile('new.xlsx');
    })

If you're using the Streams API with exceljs, you can also pipe your stream into fs.createWriteStream to write to a file as well.




回答2:


You can modify excel, below is the example.

var Excel = require('exceljs');
async function excelOp() {
    let workbook = new Excel.Workbook();
    workbook = await workbook.xlsx.readFile('question_39869739.xlsx'); // replace question_39869739.xls with your file
    let worksheet = workbook.getWorksheet('sheetname'); // replace sheetname with actual sheet name
    worksheet.getRow('rowNumber').getCell('cellNumber').value = 350; // replace rowNumber and cellNumber with the row and cell you want to modify
    workbook.xlsx.writeFile('question_50508131.xlsx');
}

excelOp();

Have a look at https://www.npmjs.com/package/exceljs#interface for all the possible operations with exceljs.



来源:https://stackoverflow.com/questions/39869739/modify-existing-excel-file-using-node-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!