SheetJS / sheetjs

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs
https://sheetjs.com/
Apache License 2.0
35.13k stars 8k forks source link

Corrupted XLSM file #2115

Closed leanesanity closed 4 years ago

leanesanity commented 4 years ago

I have an issue when downloading the file, what I am suppose to do is edit the content of my xlsm file and then download the file. I also tried https://github.com/SheetJS/sheetjs/issues/217 workaround here, but it doesn't work.

My Sample Code :

export const insertData = (file, callback) => { const fileReader = new FileReader(); fileReader.onload = (event) => { try { const { result } = event.target; const workbook = XLSX.read(result, { type: 'binary', }); const firstSheet = workbook.SheetNames[0]; const worksheet = workbook.Sheets[firstSheet]; // modify value in D4 worksheet.D4.v = 'NEW VALUE from NODE';

  const wopts = { bookType: 'xlsx', bookSST: true, type: 'binary' };
  const wbout = XLSX.write(workbook, wopts);
  /* generate array buffer */
  console.log(wbout);  // Weird characters are found
  /* create data URL */
  const url = URL.createObjectURL(
    new Blob([wbout], {
      type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    }),
  );
  callback(URL); // URL here is use for download
} catch (e) {
  console.log(e);
  callback(null);
}

}; fileReader.readAsBinaryString(file); };

LOG image

SheetJSDev commented 4 years ago

Let's start from the top. Can you try:

export const insertData = (file, callback) => {
  const fileReader = new FileReader();
  fileReader.onload = (event) => {
    try {
      const { result } = event.target;
      const workbook = XLSX.read(result, {
        type: 'binary',
      });
      const firstSheet = workbook.SheetNames[0];
      const worksheet = workbook.Sheets[firstSheet];
      // Log D4
      console.log(worksheet.D4);
      // modify value in D4
      worksheet.D4.v = 'NEW VALUE from NODE';

      const wopts = { bookSST: true };
      XLSX.writeFile(workbook, "out.xlsm", wopts);
      callback(true); // URL here is use for download
    } catch (e) {
      console.log(e);
      callback(null);
    }
  };
  fileReader.readAsBinaryString(file);
};

If this fails, can you share the original and generated file?

leanesanity commented 4 years ago

That code ables me to download the file. But it seems that the formats on my file are all gone and macros

leanesanity commented 4 years ago

What I am trying to do here is just, update some values of some cells. Retaining the same formats of the file. Is it possible ? How to do it? Thank you very much for the help. Really appreciated it

SheetJSDev commented 4 years ago

That particular work flow is something we offer as part of our Pro offering.

mytrytest commented 3 years ago

I am also getting a corrupted excel file. I am using the express JS server and trying to get from AWS MWS. API will return excel but I am not able to get a proper Excel file. Anyone can help me?

const XLSX = require('xlsx');
var ws = XLSX.utils.json_to_sheet(response.data);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws);

var buf = XLSX.write(wb, {bookType:'xlsx', type:'buffer'}); // generate a nodejs buffer
var str = XLSX.write(wb, {bookType:'xlsx', type:'binary'});

XLSX.writeFile(wb, "sheetjs.xlsx");
mytrytest commented 3 years ago

I fixed the issue by write stream.

const dest = fs.createWriteStream('./sheetjs.xlsx');
response.body.pipe(dest).on('finish', function(){
  console.log('done');
});