javascript 将 table 导出 Excel ,可跨行跨列

<script language="JavaScript" type="text/javascript">
        //jQuery HTML导出Excel文件(兼容IE及所有浏览器)
        function HtmlExportToExcel(tableid,file_name) {
            var filename =file_name; //'Book'
            if (getExplorer() == 'ie' || getExplorer() == undefined) {
                HtmlExportToExcelForIE(tableid, filename);
            }
            else {
                HtmlExportToExcelForEntire(tableid, filename)
            }
        }
        //IE浏览器导出Excel
        function HtmlExportToExcelForIE(tableid, filename) {
            try {            
                var curTbl = document.getElementById(tableid); 
                var oXL; 
                try
                    oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel 
                }catch(e){ 
                    alert("无法启动Excel!\n\n如果您确信您的电脑中已经安装了Excel,"+"那么请调整IE的安全级别。\n\n具体操作:\n\n"+"工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用"); 
                    return false
                
                var oWB = oXL.Workbooks.Add(); //获取workbook对象 
                var oSheet = oWB.ActiveSheet;//激活当前sheet 
                var sel = document.body.createTextRange(); 
                sel.moveToElementText(curTbl); //把表格中的内容移到TextRange中 
                try{
                    sel.select(); //全选TextRange中内容 
                }catch(e1){
                    e1.description
                }
                sel.execCommand("Copy");//复制TextRange中内容 
                oSheet.Paste();//粘贴到活动的EXCEL中 
                oXL.Visible = true; //设置excel可见属性 
                var fname = oXL.Application.GetSaveAsFilename(filename+".xls", "Excel Spreadsheets (*.xls), *.xls"); 
                oWB.SaveAs(fname); 
                oWB.Close(); 
                oXL.Quit();
 
            } catch (e) {
                alert(e.description);
            }
        }
         
        //非IE浏览器导出Excel
        var HtmlExportToExcelForEntire = (function() {
            var uri = 'data:application/vnd.ms-excel;base64,',
        template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
        base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) },
        format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
            return function(table, name) {
                if (!table.nodeType) { table = document.getElementById(table); }
                var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
                document.getElementById("dlink").href = uri + base64(format(template, ctx));
                document.getElementById("dlink").download = name + ".xls";
                document.getElementById("dlink").click();
            }
        })()
         
        function getExplorer() {
            var explorer = window.navigator.userAgent;
            //ie
            if (explorer.indexOf("MSIE") >= 0) {
                return 'ie';
            }
            //firefox
            else if (explorer.indexOf("Firefox") >= 0) {
                return 'Firefox';
            }
            //Chrome
            else if (explorer.indexOf("Chrome") >= 0) {
                return 'Chrome';
            }
            //Opera
            else if (explorer.indexOf("Opera") >= 0) {
                return 'Opera';
            }
            //Safari
            else if (explorer.indexOf("Safari") >= 0) {
                return 'Safari';
            }
        }
</script>
1
2
3
4
5
6
7
8
9
10
//调用
function ToExcel(){
      
     HtmlExportToExcel('table_main','Book');
}
 
//html部分   table_main为table的id
<table  id="table_main"    >
 
</table>

  

  

posted @   海乐学习  阅读(1153)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示