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

XLSX Generated file content is in raw form #2246

Closed muhammadali7768 closed 3 years ago

muhammadali7768 commented 3 years ago

I am getting a xlsx file from Amazon, the attached image shows the response headers and body.

Screenshot from 2021-03-04 13-03-21

I am writing the file to desk using the following code

var workbook = XLSX.read(response, {type: 'array'})
XLSX.writeFile(workbook, 'out.xlsx')

When i open the file the content is not in readable form. Each cell has a string of 000000000. Following is the screenshot of the file

Screenshot from 2021-03-05 09-12-25

moltar commented 3 years ago

Maybe it is gziped?

muhammadali7768 commented 3 years ago

yes i fixed this by making axios call. I was using a plugin which returns the response as a string and there was some bytes missing i think thats why i was getting scrambled data. With the axios call now i can save the file as a zip and .xlsx both and also can return the file to the browser.

 return axios({
        method: 'post',
        url: url,
        data: requestParameters(action, token, sellerId, {ReportId: reportId}),
        responseType: 'stream'
      })
        .then(async response => {        
          response.data.pipe(res)       
        })
        .catch(error => {
          if (error.response) {          
            console.log(error.response.data)
             console.log(error.response.status);
             console.log(error.response.headers);
          }       
        })
moltar commented 3 years ago

Is this something we can fix in the MWS package?

muhammadali7768 commented 3 years ago

I think yes. MWS package is working fine for flat file reports but i am getting scramble data for the report types "_RFQD_BULKDOWNLOAD" and "_FEE_DISCOUNTSREPORT" which are compressed xlsx files.

muhammadali7768 commented 3 years ago

When i saved "RFQD_BULK_DOWNLOAD" and "FEE_DISCOUNTS_REPORT" reports as a zip using axios. The zip file looks like this. Screenshot from 2021-03-19 11-20-17

But i can save these reports directly as .xlsx files too.

SheetJSDev commented 3 years ago

Some APIs send back UTF8 encoded strings instead of raw binary data. Since the ZIP format uses byte sequences that could be interpreted as unicode characters, the result will not be a valid ZIP.

Note that some services like AWS Lambda perform some UTF8 transform on the response by default, and some server-side configuration may be needed