87,910
社区成员
发帖
与我相关
我的任务
分享
function Save_Excel(e, c) {//导出Excel文件
var data = $(e).datagrid('getExcelXml', { title: 'datagrid import to excel', hidde: c,hidden:true }); var curr_time = new Date();
var strDate = curr_time.getFullYear() + "-";
strDate += curr_time.getMonth() + 1 + "-";
strDate += curr_time.getDate() + "-";
strDate += curr_time.getHours() + "-";
strDate += curr_time.getMinutes() + "-";
strDate += curr_time.getSeconds();
//问题主要是这个地方
var screen = window.screen, fSaveWindow = window.open('', '_blank', 'width=' + Math.round(screen.width * 0.5) + ',height=' + Math.round(screen.height * 0.5) + ',left=' + Math.round(screen.width * 0.05));
fSaveWindow.document.write(data);
if ((navigator.userAgent.indexOf('MSIE') >= 0)
&& (navigator.userAgent.indexOf('Opera') < 0)
|| (!!window.ActiveXObject || "ActiveXObject" in window)
|| (navigator.userAgent.indexOf('Edge') >= 0))//包括Microsoft IE和Microsoft Edge
{
fSaveWindow.document.execCommand("SaveAs", true, strDate + ".xls");
} //Firefox浏览器的支持
else if (navigator.userAgent.indexOf('Firefox') >= 0) {
var aLink = document.createElement('a');
var blob = new Blob([data]);
var evt = document.createEvent("HTMLEvents");
evt.initEvent("click", true, true);
aLink.download = strDate + ".xls";
aLink.href = URL.createObjectURL(blob);
aLink.dispatchEvent(evt);
document.body.appendChild(aLink);
aLink.click();
}
//其他如Chrome和360浏览器的支持
else {
var aLink = fSaveWindow.document.createElement('a');
var blob = new Blob([data]);
var evt = fSaveWindow.document.createEvent("HTMLEvents");
evt.initEvent("click", false, false);
aLink.download = strDate + ".xls";
aLink.href = URL.createObjectURL(blob);
aLink.dispatchEvent(evt);
}
fSaveWindow.close();
}
var frame = $('<iframe id="test" name="test" width="0" height="0"></iframe>');
$("body").append(frame);
var fbody = frame[0].contentWindow;
fbody.document.open("text/html", "replace");
fbody.document.write(data);
fbody.document.close();
fbody.focus();
//fbody.print();
fbody.document.execCommand("SaveAs", true, strDate + ".xls");
frame.remove();