js 导出到excel 最新兼容火狐谷歌ie11

function isIE() { //ie?
            if (!!window.ActiveXObject || "ActiveXObject" in window)
                return true;
            else
                return false;
        }
        function getXlsFromTbl(inTblId, inWindow) {
            try {
                var allStr = "";
                var curStr = "";
                var fileName = getExcelFileName();
                //                var cc = document.all;
                //                var ccc = typeof (document.all)
                //                alert(cc)
                //                alert(ccc)
                if (inTblId != null && inTblId != "" && inTblId != "null") {
                    var hh = isIE();
                    if (hh == true) //IE浏览器
                    {
                        curStr = getTblData(inTblId, inWindow);
                        if (curStr != null) {
                            allStr += curStr;
                        }
                        else {
                            alert("你要导出的表不存在!");
                            return;
                        }
                        doFileExport(fileName, allStr);
                    } else {
                        curStr = getTblData1(inTblId, inWindow);
                        if (curStr != null) {
                            allStr += curStr;
                        }
                        else {
                            alert("你要导出的表不存在!");
                            return;
                        }
                        var uri = ‘data:text/xls;charset=utf-8,\ufeff‘ + encodeURIComponent(allStr);
                        //创建a标签模拟点击下载
                        var downloadLink = document.createElement("a");
                        downloadLink.href = uri;
                        downloadLink.download = fileName;
                        document.body.appendChild(downloadLink);
                        downloadLink.click();
                        document.body.removeChild(downloadLink);
                    }
                }
            }
            catch (e) {
                alert("导出发生异常:" + e.name + "->" + e.description + "!");
            }
        }
        function getTblData(inTbl, inWindow) {
            var rows = 0;
            //alert("getTblData is " + inWindow);
            var tblDocument = document;
            if (!!inWindow && inWindow != "") {
                if (!document.all(inWindow)) {
                    return null;
                }
                else {
                    tblDocument = eval(inWindow).document;
                }
            }
            var curTbl = tblDocument.getElementById(inTbl);
            var outStr = "";
            if (curTbl != null) {
                for (var j = 0; j < curTbl.rows.length; j++) {
                    //alert("j is " + j);
                    for (var i = 0; i < curTbl.rows[j].cells.length; i++) {
                        //alert("i is " + i);
                        if (i == 0 && rows > 0) {
                            outStr += " \t";
                            rows -= 1;
                        }
                        outStr += curTbl.rows[j].cells[i].innerHTML + " \t";
                        if (curTbl.rows[j].cells[i].colSpan > 1) {
                            for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {
                                outStr += " \t";
                            }
                        }
                        if (i == 0) {
                            if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {
                                rows = curTbl.rows[j].cells[i].rowSpan - 1;
                            }
                        }
                    }
                    outStr += "\r\n";
                }
            }
            else {
                outStr = null;
                alert(inTbl + "不存在!");
            }
            return outStr;
        }
        function getTblData1(inTbl, inWindow) {
            var rows = 0;
            var tblDocument = document;
            //tblDocument = eval(inWindow).document;
            var curTbl = tblDocument.getElementById(inTbl);
            var outStr = "";
            if (curTbl != null) {
                for (var j = 0; j < curTbl.rows.length; j++) {
                    for (var i = 0; i < curTbl.rows[j].cells.length; i++) {

if (i == 0 && rows > 0) {
                            outStr += ","; // \t";
                            rows -= 1;
                        }

outStr += curTbl.rows[j].cells[i].innerHTML + ","; //\t";
                        if (curTbl.rows[j].cells[i].colSpan > 1) {
                            for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {
                                outStr += ","; // \t";
                            }
                        }
                        if (i == 0) {
                            if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {
                                rows = curTbl.rows[j].cells[i].rowSpan - 1;
                            }
                        }
                    }
                    outStr += "\r\n";
                }
            }
            else {
                outStr = null;
                alert(inTbl + "不存在 !");
            }
            return outStr;
        }
        function getExcelFileName() {
            var d = new Date();
            var curYear = d.getYear();
            var curMonth = "" + (d.getMonth() + 1);
            var curDate = "" + d.getDate();
            var curHour = "" + d.getHours();
            var curMinute = "" + d.getMinutes();
            var curSecond = "" + d.getSeconds();
            if (curMonth.length == 1) {
                curMonth = "0" + curMonth;
            }
            if (curDate.length == 1) {
                curDate = "0" + curDate;
            }
            if (curHour.length == 1) {
                curHour = "0" + curHour;
            }
            if (curMinute.length == 1) {
                curMinute = "0" + curMinute;
            }
            if (curSecond.length == 1) {
                curSecond = "0" + curSecond;
            }
            var fileName = "阳光印网" + "_" + curYear + curMonth + curDate + "_"
            + curHour + curMinute + curSecond + ".xls";
            //alert(fileName);
            return fileName;
        }
        function doFileExport(inName, inStr) {
            var xlsWin = null;
            if (!!document.all("glbHideFrm")) {
                xlsWin = glbHideFrm;
            }
            else {
                var width = 6;
                var height = 4;
                var openPara = "left=" + (window.screen.width / 2 - width / 2)
   + ",top=" + (window.screen.height / 2 - height / 2)
   + ",scrollbars=no,width=" + width + ",height=" + height;
                xlsWin = window.open("", "_blank", openPara);
            }
            xlsWin.document.write(inStr);
            xlsWin.document.close();
            xlsWin.document.execCommand(‘Saveas‘, true, inName);
            xlsWin.close();
        }

时间: 2024-10-14 15:52:22

js 导出到excel 最新兼容火狐谷歌ie11的相关文章

js快速判断IE浏览器(兼容IE10与IE11)

在很多时候,我们一般采用navigator.userAgent和正则表达来判断IE浏览器版本,下面介绍用IE浏览器中不同特性来判断IE浏览器   1 判断IE浏览器与非IE 浏览器 IE浏览器与非IE浏览器的区别是IE浏览器支持ActiveXObject,但是非IE浏览器不支持ActiveXObject.在IE11浏览器还没出现的时候我们判断IE和非IE经常是这么写的 ? 1 2 3 function isIe(){        return window.ActiveXObject ? tr

将HTML表格导出到EXCEL,兼容Firefox,支持中文

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <meta http-equiv="Content

js导出到excel 兼容ie11火狐,谷歌,360

<script language="javascript" type="text/javascript">         function isIE() { //ie?             if (!!window.ActiveXObject || "ActiveXObject" in window)                 return true;             else                 re

js 导出到excel

/* * 默认转换实现函数,如果需要其他功能,需自行扩展 * 参数: * tableID : HTML中Table对象id属性值 * 详细用法参见以下 TableToExcel 对象定义 */ function saveAsExcel(tableID) { var tb = new TableToExcel(tableID); tb.setFontStyle("Courier New"); tb.setFontSize(10); tb.setTableBorder(2); tb.set

js导出table中的EXCEL总结

导出EXCEL一般是用PHP做,但是项目中,有时候PHP后端工程师返回的数据不是我们想要的,作为前端开发工程师,把对应的数据编号转换为文字后,展示给用户,但是,需求要把数据同时导出一份EXCEl.无奈之下,我只能用js导出table中的数据了. 导出EXCEl一般是自己人用的,所以用js导出,因为js导出EXCEL一般情况下兼容性不是很好,很多只是兼容IE浏览器,还要设置在工具栏中进行设置才能导出,因为会相对比较烦.下面介绍几种方法: 一.js导出EXCEl带单元格合并[已验证,比较好用] //

HTML导出Excel文件(兼容IE及所有浏览器)

注意:IE浏览器需要以下设置: 打开IE,在常用工具栏中选择"工具"--->Internet选项---->选择"安全"标签页--->选择"自定义级别"--->弹出的窗口中找到ActiveX控件和插件--->将"ActiveX控件自动提示""对没有标记安全的ActiveX控件进行初始化和脚本运行""下载未签名的ActiveX控件"三项 选择启用 即可! Htm

JS导出数据到EXCEL

1.用到两个文件Blob.js和FileSaver.js 2.代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8"/> <title>js导出excel-ComingX</title> </head> <body> <h1> </h1> <a> <p&g

js导出excel增加表头、mso-number-format定义数据格式

问题1:增加表头 js导出表格时,只会导出table里的展现出的内容,如需增加表头等内容需要在页面获取的字符串中拼接表头的相关字符串,详细介绍如下: tableString:新增的表头内容字符串: ctx.table: 新增的表头+页面获取的table字符串. 这样,表头就加上了,还可以通过style设置它的样式: 问题2:mso-number-format定义数据格式 在做项目时遇到mso-number-format定义的科学计数法转化格式与金额定义的向右对齐样式有冲突.  之前有写过篇博客专

js 导出到word,excel

1>js导出,原理:复制页面中的table,让后粘贴到excle中 function exportToWord(id) { //Scripting.FileSystemObject (FSO 文本文件读写)被关闭了, //开启FSO功能即可,在"运行"中执行regsvr32 scrrun.dll即可 try { var oElement = document.getElementById(id); var word = new ActiveXObject("Word.A