Download XLSX getting corrupted

99封情书 提交于 2019-12-07 08:45:45

问题


I'm trying to convert a HTML table to XLSX uisng AngularJS or even plain JavaScript.

When I'm converting to XLS using below its downloading fine and a XLS file is opening.

var blob = new Blob([template], {type: "data:application/vnd.ms-excel"});
window.navigator.msSaveOrOpenBlob(blob, "myExcel.xls"); 

But, my requirement wants the file in XLSX, so I changed the MIME type and extension of file in the same code like below,

var blob = new Blob([template], {type: "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
window.navigator.msSaveOrOpenBlob(blob, "myExcel.xlsx");

But when I'm opening the downloaded xlsx file its corrupted and MS office is trowing error popup.

What I'm doing wrong and how I can convert a HTML table to XLSX? Even alternate ideas also accepted.

PS: The table is quite complicated, rows within columns and all, so plugins like ALASQL, JS-XLSX is not helping.


回答1:


The problem may lie in the type XLSX. This post states that it is a zipped set of XLS files. Have a read through. I cannot comment, because my score isn't over 50 yet, so I'm posting as an answer.

https://stackoverflow.com/a/37516332/1425168




回答2:


just try to change mime-type

change it to application/vnd.ms-excel it works fine with XLSX also.

var blob = new Blob([template], {type: "application/vnd.ms-excel"});
window.navigator.msSaveOrOpenBlob(blob, "myExcel.xlsx");

Try this code.



来源:https://stackoverflow.com/questions/44559457/download-xlsx-getting-corrupted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!