admin管理员组文章数量:1415664
I want to save a html table in a excel file, I used fileSaver for angular. But when I download the file and try to open it I receive an annoying popup from excel which say
"The file format and extension of 'report.xls' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?"
$scope.exportExcel = function(){
var data, table;
table = document.getElementById('tableReport').innerHTML;
data = new Blob([table], {
type: 'application/vnd.ms-excel;charset=charset=utf-8'
});
return FileSaver.saveAs(data, 'report.xls');
};
How can I fix this popup? Thanks.
I want to save a html table in a excel file, I used fileSaver for angular. But when I download the file and try to open it I receive an annoying popup from excel which say
"The file format and extension of 'report.xls' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?"
$scope.exportExcel = function(){
var data, table;
table = document.getElementById('tableReport').innerHTML;
data = new Blob([table], {
type: 'application/vnd.ms-excel;charset=charset=utf-8'
});
return FileSaver.saveAs(data, 'report.xls');
};
How can I fix this popup? Thanks.
Share Improve this question edited Apr 29, 2016 at 5:37 radjiv asked Apr 29, 2016 at 4:32 radjivradjiv 1192 gold badges2 silver badges12 bronze badges 4- The proper extension for that format is .htm not .xls – Musa Commented Apr 29, 2016 at 12:08
- @musa: thx for your answer but I want to extract it in excel not html – radjiv Commented May 2, 2016 at 0:59
- It is html, and excel expects a .htm extension for that type of file. Specifying an extension is not going to convert data from one format to another – Musa Commented May 2, 2016 at 1:13
- I found this answer very helpful stackoverflow./a/50819212/414744 – Ravi Ranjan Commented Oct 31, 2018 at 11:48
1 Answer
Reset to default 1I had a similar issue.
I figured that I forgot to set the responseType
to blob
on my HTTP request.
This seems to be required in order to read binary data correctly.
Example:
$http({
url: 'your/webservice',
method: "POST",
data: json, //this is your json data string
headers: {
'Content-type': 'application/json'
},
responseType: 'blob'
}).success(function (data, status, headers, config) {
var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
}).error(function (data, status, headers, config) {
//upload failed
});
The code es from this answer.
本文标签: javascriptThe file format and extension of 39filexls39 don39t match with fileSaverjsStack Overflow
版权声明:本文标题:javascript - The file format and extension of 'file.xls' don't match with fileSaver.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745207075a2647689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论