我的客户端是WIN7,IE8
在用document.execCommand("saveas",true,"name.xls");
导出后的界面,支持的文件格式只有txt,htm,html。
我依照IE7的修改方法:
[HKEY_CLASSES_ROOT.xls] “PerceivedType”=”document”
[HKEY_CLASSES_ROOT.xlt] “PerceivedType”=”document”
还是没用,请大神们帮忙解决。
http://stackoverflow.com/questions/2515791/execcommandsaveas-null-file-csv-is-not-working-in-ie8
最好是上传内容到服务器后输出content-disposition响应头来实现其他文件类型的保存
走服务端的已经实现了,现在请教的问题是前台js导出问题。
另,服务端下载的弊端是数据量大的话,还得再传输回后台,传回前台。
贴一段代码供大家测试
IE8
<!DOCTYPE html>
编号 | 姓名 | 年龄 | 性别 |
---|---|---|---|
3 | 张三1 | 11 | 男 |
2 | 张三2 | 11 | 男 |
1 | 张三3 | 11 | 男 |
4 | 张三4 | 11 | 男 |
参考 IE9 - no way to save the results of CSV export #2312
/**
* @ngdoc function
* @name isIEBelow10
* @methodOf ui.grid.exporter.service:uiGridExporterService
* @description Checks whether current browser is IE and of version below 10
*/
isIEBelow10: function () {
var myNav = navigator.userAgent.toLowerCase();
return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) < 10 : false;
},
/**
* @ngdoc function
* @name downloadFile
* @methodOf ui.grid.exporter.service:uiGridExporterService
* @description Triggers download of a csv file. Logic provided
* by @cssensei (from his colleagues at https://github.com/ifeelgoods) in issue #2391
* @param {string} fileName the filename we'd like our file to be
* given
* @param {string} csvContent the csv content that we'd like to
* download as a file
*/
downloadFile: function (fileName, csvContent) {
var D = document;
var a = D.createElement('a');
var strMimeType = 'application/octet-stream;charset=utf-8';
var rawFile;
if (!fileName) {
var currentDate = new Date();
fileName = "CSV Export - " + currentDate.getFullYear() + (currentDate.getMonth() + 1) +
currentDate.getDate() + currentDate.getHours() +
currentDate.getMinutes() + currentDate.getSeconds() + ".csv";
}
if (this.isIEBelow10()) {
var frame = D.createElement('iframe');
document.body.appendChild(frame);
frame.contentWindow.document.open("text/html", "replace");
frame.contentWindow.document.write('sep=,\r\n' + csvContent);
frame.contentWindow.document.close();
frame.contentWindow.focus();
frame.contentWindow.document.execCommand('SaveAs', true, fileName);
document.body.removeChild(frame);
return true;
}
// IE10+
if (navigator.msSaveBlob) {
return navigator.msSaveBlob(new Blob(["\ufeff", csvContent], {
type: strMimeType
}), fileName);
}
//html5 A[download]
if ('download' in a) {
var blob = new Blob([csvContent], {
type: strMimeType
});
rawFile = URL.createObjectURL(blob);
a.setAttribute('download', fileName);
} else {
rawFile = 'data:' + strMimeType + ',' + encodeURIComponent(csvContent);
a.setAttribute('target', '_blank');
a.setAttribute('download', fileName);
}
a.href = rawFile;
a.setAttribute('style', 'display:none;');
D.body.appendChild(a);
setTimeout(function() {
if (a.click) {
a.click();
// Workaround for Safari 5
} else if (document.createEvent) {
var eventObj = document.createEvent('MouseEvents');
eventObj.initEvent('click', true, true);
a.dispatchEvent(eventObj);
}
D.body.removeChild(a);
}, 100);
}
感谢save4me
execcommand在IE8下导出是,格式还是只支持txt,htm,html三种。