通过js导出Excel文件

最近要把生成的table导出到Excel里面,在网上搜了两种方法

1.采用插件jquery.table2excel.js

首先在jsp页面导入

<script src="<%=contextpath%>/static/js/jquery-3.2.1.min.js"></script>
<script src="<%=contextpath%>/static/js/jquery.table2excel.js"></script>
然后有一个按钮,和一个table  

<input type="button" class="exportBtn" style="width:100px;height:30px;background-color: darkcyan;" id="exportDevLedger" value="导出Excel"/>
<table   class="table table-out-bordered table-bordered table-hover" id="tableShowAllInfo">
但是注意因为是在页面上面显示的,可能会包含一些不需要被导出的东西,例如

<th class="noExl">修改操作</th> 一些操作和其对应的处理
<td class="noExl"><input type="button" οnclick="XXX('strvalue')" value="修改"/></td>
然后在js文件里面

$("#exportDevLedger").click(function () {  //通过插件导成Excel文件  方法二 采用
    // alert("生成Excel!");
    $("#tableShowAllInfo").table2excel({
        exclude: ".noExl",  //class = "noExl"的不被导出
        name: "Excel Document Name",
        filename: "设备信息", //生成的文件名
        exclude_img: true,
        exclude_links: true,
        exclude_inputs: true
    })
});
这样第一种方案就可以了
#######################################################################
方法二
在页面上面重新创建一个table并设置为hidden,是为了不要页面上的某些不需要导出的信息,这些东西不在这个表上面出现
同样需要一个input按钮和一个table
<input type="button" class="exportBtn" style="width:100px;height:30px;background-color: darkcyan;" οnclick="exportDevLedger('tableForExport')" value="导出Excel"/>
<table id="tableForExport" hidden>
然后在js文件里面
var idTmr;
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';
    }
}
function exportDevLedger(tableid) {
    if(getExplorer()=='ie')//如果使用的是ie浏览器
    {
        var curTbl = document.getElementById(tableid);
        var oXL = new ActiveXObject("Excel.Application");
        var oWB = oXL.Workbooks.Add();
        var xlsheet = oWB.Worksheets(1);
        var sel = document.body.createTextRange();
        sel.moveToElementText(curTbl);
        sel.select();
        sel.execCommand("Copy");
        xlsheet.Paste();
        oXL.Visible = true;

        try {
            var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
        } catch (e) {
            print("Nested catch caught " + e);
        } finally {
            oWB.SaveAs(fname);
            oWB.Close(savechanges = false);
            oXL.Quit();
            oXL = null;
            idTmr = window.setInterval("Cleanup();", 1);
        }

    }
    else   //非IE浏览器
    {
        tableToExcel(tableid)
    }
}
function Cleanup() {
    window.clearInterval(idTmr);
    CollectGarbage();
}
var tableToExcel = (function() {
    var uri = 'data:application/vnd.ms-excel;base64,',
        template = '<html><head><meta charset="UTF-8"></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};
        window.location.href = uri + base64(format(template, ctx))
    }
})();
说明:这些都是我看其他人的,总结了一下,希望对大家有帮助


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值