Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use in browser - issue while opening generated excel #18

Open
ikochetkov opened this issue Apr 7, 2021 · 2 comments
Open

Use in browser - issue while opening generated excel #18

ikochetkov opened this issue Apr 7, 2021 · 2 comments

Comments

@ikochetkov
Copy link

ikochetkov commented Apr 7, 2021

Using your code from README.md - successfully generated workbook and got in running in dev(Thanks for this!) (locally, and in dev it works, however , after deployment to TEST env it downloaded and says me when I'm trying to open it: Excel could not be opened....because file format or file extension is not valid.
Difference in size: 2kb, 116 vs 118(valid)
I had same issue with XLSX library, that time I used different bookType - changed xlsx to xls - and it was fine ( XLSX.writeFile(workbook, statements_export_${new Date().toUTCString()}.xls, { bookType: 'xls' });
Any suggestions for my case?
image
NOTwork.xlsx
works.xlsx

P.S: by the way interesting that both excels have content type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet instead of that which we set in mimeType - application/vnd.ms-excel

@pietersv
Copy link
Collaborator

pietersv commented Apr 7, 2021

As a test, can you try renaming the file to something simpler? I've often found that some characters can confuse either Excel on open or confuse the browser when interpreting http headers on download. The other thing I notice is maybe it's a mismatch between .xlsx extension (with an x indicating it's Office XML file) and booktype 'xls' (without the x indicating its an older binary file)

@ikochetkov
Copy link
Author

Hi Pieter, thanks for collaboration.
I just changed name, but same error unfortunetly (in dev it's again works, it test no). This is the end of my code block for excel...

workbook.generate(function (err, jszip) {
          if (err) return callback(err);

          jszip.generateAsync({ type: "blob", mimeType: 'application/vnd.ms-excel' }).then(function (blob) {
            var filename = 'text.xlsx'
            if (navigator.msSaveBlob) { // IE 10+
              navigator.msSaveBlob(blob, filename);
            }
            else {
              var link = document.createElement("a");
              if (link.download !== undefined) { // feature detection
                // Browsers that support HTML5 download attribute
                var url = URL.createObjectURL(blob);
                link.setAttribute("href", url);
                link.setAttribute("download", filename);
                link.style.visibility = 'hidden';
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
              }
            }
          })
        });

I thought maybe this property mimeType is similar to bookType in SheetJS lib (XLSX through npm, where I was able to get it work with changing this booktype...), because I played with mimeType - tried to change it, but nothing happens. Maybe there is a way to convert it to old booktype with 'xls' somehow (maybe then it will work in my case)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants