JS 导出表格
来源网络,作了简单优化优化
//导出excel function toExcel(bookname,targetControlerID) { //window.location.href='<%=basePath%>pmb/excelShowInfo.do'; //获取表格 try { var exportFileContent = document.getElementById(targetControlerID).outerHTML; } catch (e) { alert("请查询后再点击导出!"); return; } //设置格式为Excel,表格内容通过btoa转化为base64,此方法只在文件较小时使用(小于1M) //exportFileContent=window.btoa(unescape(encodeURIComponent(exportFileContent))); //var link = "data:"+MIMEType+";base64," + exportFileContent; //使用Blob var blob = new Blob([exportFileContent], { type: "text/plain;charset=utf-8" }); //解决中文乱码问题 blob = new Blob([String.fromCharCode(0xFEFF), blob], { type: blob.type }); if ("msSaveOrOpenBlob" in navigator) { //兼容ie window.navigator.msSaveOrOpenBlob(blob, bookname); return; } //设置链接 var link = window.URL.createObjectURL(blob); var a = document.createElement("a"); //创建a标签 a.download = bookname; //设置被下载的超链接目标(文件名) a.href = link; //设置a标签的链接 document.body.appendChild(a); //a标签添加到页面 a.click(); //设置a标签触发单击事件 document.body.removeChild(a); //移除a标签 }
使用在前端需要导出的button上添加 onclick="toExcel('DieChecked.xls', 'MainContent_gdvDieChecked') 即可……
*****有道无术,术尚可求;有术无道,止于术。*****