zoukankan      html  css  js  c++  java
  • 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') 即可……

  • 相关阅读:
    CSS盒子模型
    getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
    MYSQL中的CASE WHEN END AS
    单点登录的精华总结
    git&github
    June 21st 2017 Week 25th Wednesday
    June 20th 2017 Week 25th Tuesday
    June 19th 2017 Week 25th Monday
    June 18th 2017 Week 25th Sunday
    June 17th 2017 Week 24th Saturday
  • 原文地址:https://www.cnblogs.com/chengcanghai/p/14174499.html
Copyright © 2011-2022 走看看