pbalasimon
pbalasimon

Reputation: 61

Generate Base64 from ExcelJS WorkBook

I am working with ExcelJS library in NodeJS for creating a excel file. What I need it's to generate a Base64 string from a Workbook object.

I have this code

let workbook = new Excel.stream.xlsx.WorkbookWriter({});
let worksheet = workbook.addWorksheet(`CREReport_${origcontractid}`);
worksheet.getCell('A1').value = 'CRE evolution';
worksheet.commit(); 

but I don't know how to generate a Base64 string based on my workbook. I don't want to create a file in my disk, i want to return in my webservice a base64 string which represent the file. any idea?

Thanks!

Upvotes: 3

Views: 6425

Answers (2)

Xunauth
Xunauth

Reputation: 1

I did this. Just need writebuffer method and create blob object using data buffer. Whit this, we can create base64 string from workbook.

workbook.xlsx.writeBuffer().then(async (data) => {
  const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
  const reader = new FileReader();
  reader.readAsDataURL(blob); 
  reader.onloadend = function() {
    const base64data = reader.result;                
    console.log(base64data);
  }
});

Good luck

Upvotes: 0

Jorge Rolhas
Jorge Rolhas

Reputation: 61

with the file buffer you can get the base64.

const fileBuffer = await workbook.xlsx.writeBuffer()

Upvotes: 6

Related Questions