protobi / js-xlsx

XLSX / XLSM / XLSB (Excel 2007+ Spreadsheet) / ODS parser and writer
http://oss.sheetjs.com/js-xlsx
Other
814 stars 416 forks source link

Not able to keep style cells after modification of the excel file #109

Open MatthieuAvrgs opened 6 years ago

MatthieuAvrgs commented 6 years ago

Hi everyone,

I have the same issue with the cells style. When I try to read a xlsx file, modify it and force the download of the file, it doesn't keep the style. Here is my code:

`const stream = data.Body; //Uint8Array const arr = new Array();

        for (let i = 0; i !== stream.length; ++i) {
          arr[i] = String.fromCharCode(stream[i]);
        }

        const bstr = arr.join('');
        const workbook = XLSX.read(bstr, {
          type: 'binary',
          cellStyles: true
        });
        const worksheet = workbook.Sheets['YYYY'];
        worksheet.C11.v = 'XXXX';
        const wopts = {
          bookType: 'xlsx',
          type: 'binary',
        };
        const wbout = XLSX.write(workbook, wopts);
        const buf = new ArrayBuffer(wbout.length);
        const view = new Uint8Array(buf);

        for (let i = 0; i !== wbout.length; ++i) {
          view[i] = wbout.charCodeAt(i) & 0xff; //eslint-disable-line no-bitwise
        }

        FileSaver.saveAs(new Blob([buf], { type: 'application/octet-stream' }), 'test.xlsx');`

stream is a Uint8Array array I guess the problem is with the parameters of XLSX.write and XLSX.read because I am able to keep the cells style when I don't pass through them. But in fact I need to use them because I have to modify the content of few cells. Does someone have an idea to fix this?