Welcome 微信登录

首页 / 脚本样式 / JavaScript / JavaScript将Html中的Table表格导出为Excel的方法

JavaScript将Html中的Table表格导出为Excel的方法

function toExcel(){
    //window.location.href='<%=basePath%>pmb/excelShowInfo.do';
    //获取表格
    var exportFileContent = document.getElementById("datatable").outerHTML;
    //设置格式为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});
    //设置链接
    var link = window.URL.createObjectURL(blob);
    var a = document.createElement("a");    //创建a标签
    a.download = "监测网址列表.xls";  //设置被下载的超链接目标(文件名)
    a.href = link;       ...        
该文章为易网时代-编程资源站会员专属文章,请先登录后再进行查看。