0
// read from a file
const workbook = new Excel.Workbook();
await workbook.xlsx.readFile(filename);
// ... use workbook

As per the exceljs documentation, it should load the already existing file 'filename', but when I tried reading it,

const sheet = workbook.addWorksheet('My Sheet'); 

sheet was actually undefined.

My concern is, is it possible to do read from and write to a file operations in ReactJS, also I came across another article

https://stackoverflow.com/questions/49491710/can-reactjs-write-into-file#:~:text=React%20runs%20in%20browser%20so,gets%20served%20to%20the%20browser

that suggest it is not possible

1 Answers1

0

My project did read and write on the same file template using exceljs in node js. Hope can help you.

workbook.xlsx.readFile(template)
  .then(function () {
    // machines
    var wsMachine = workbook.getWorksheet('No');
    wsTemplate = _.cloneDeep(wsMachine);
    for (var index = 1; index <= 10; index++) {
      var copySheet = workbook.addWorksheet('No' + index, { state: 'visible' });
      copySheet.pageSetup.printArea = 'A1:FE219';
      var ws = _.cloneDeep(wsTemplate);
      copySheet.model = Object.assign(ws.model, {
        mergeCells: ws.model.merges
      });
      copySheet.name = 'No' + index;
    }

    wsMachine.state = 'hidden';
    wsTemplate.state = 'hidden';
    return workbook.xlsx.writeFile(urlOutput);
  })
  .then(function () {
    return true
  })
  .catch(function () {
    return false;
  });

Happy code

Tính Ngô Quang
  • 4,400
  • 1
  • 33
  • 33