Jquery datatables导出Excle表的问题

Jquery datatables导出Excle表的问题,第1张

FileSaverjs,如果有浏览器不支持Blob的,还需要引入Blobjs,来做导出。HTML内容的,构造一个base64字符串的路径,跳转地址下载,其实也可以将数据抽出来,用纯数据的方式。但这种导出table表格的数据在IE上测试是无法兼容的,而且在使用的过程中,一旦点击了导出,我的分页会没有用,导致分页没用的原因是因为我使用了固定列,导出excel会导出两层的数据,先删除了div在加上div来避免导出重复的数据。

所以为了更好的导出excel数据,datatables本身就提供了导出

excel、csv、pdf等格式的功能,参考如下链接:/extensions/buttons/examples/html5/excelTextBoldhtml(官方的例子导出excel,

有源码和所需的文件):/extensions/buttons/examples/initialisation/exporthtml(导出csv/pdf/copy/打印)

1所需的js文件:

//code/jquery-1124js

/11013/js/jquerydataTablesminjs

/buttons/124/js/dataTablesbuttonsminjs

//cdnjs/ajax/libs/jszip/250/jszipminjs

//cdn/buttons/124/js/buttonshtml5minjs

$(document)ready(function() {

$('#example')DataTable( {

dom: 'Bfrtip',

"buttons": [

{

'extend': 'excel',

'text': '导出',//定义导出excel按钮的文字

'exportOptions': {

'modifier': {

'page': 'current'

}

}

}

],

} );

} );

2所需的css:

/11013/css/jquerydataTablesmincss

/buttons/124/css/buttonsdataTablesmincss

3html代码:

<table id="example" class="display nowrap" cellspacing="0" width="100%">

<thead>

<tr>

<th>Name</th>

<th>Position</th>

<th>Office</th>

<th>Age</th>

<th>Start date</th>

<th>Salary</th>

</tr>

</thead>

<tfoot>

<tr>

<th>Name</th>

<th>Position</th>

<th>Office</th>

<th>Age</th>

<th>Start date</th>

<th>Salary</th>

</tr>

</tfoot>

<tbody>

<tr>

<td>Tiger Nixon</td>

<td>System Architect</td>

<td>Edinburgh</td>

<td>61</td>

<td>2011/04/25</td>

<td>$320,800</td>

</tr>

<tr>

<td>Garrett Winters</td>

<td>Accountant</td>

<td>Tokyo</td>

<td>63</td>

<td>2011/07/25</td>

<td>$170,750</td>

</tr>

</tbody>

</thead>

</table>

你好!

exclude_inputs 这个属性设置的是: 是否导出输入框中的内容

也就是说为true才是导出输入框中的内容$("#btn")click(function () { //点击按钮执行导出 

    $("table tr")each(function(i, _tr){//循环表格的行

        var tds = $(_tr)find("td");//得到每行的所有列

        tdseach(function(j, _td){//循环每列

            var _ipt = $(_td)find("input");//查找每列当中的INPUT控件

            var hasInput = (_iptlength>0)1:0;

            if(hasInput) {

                //consolelog('第'+(i+1)+'行'+(j+1)+'列含有INPUT控件');

                _iptattr("value", _iptval());//如果有INPUT控件就给它设置value值

            }

        });

    });

    $("table")table2excel({ //选取导出的表格

        filename: "项目", //导出的文件名 

        exclude_img: true, //是否导出 

        exclude_links: true,

        exclude_inputs: true

    });

})代码不复杂,看看注释就好了。

为什么要这么复杂的设置INPUT控件的value值,因为table2exceljs的源码当中是获取INPUT的value属性值,

但它得到的并不是动态赋值后的value值,所以你的数据无法导出到excel当中当然这种做法只是一种解决方案,最好结合你的实际情况去使用,修改源码当然也可以建议以后遇到类似这种问题,最好先看下源码,了解下它的执行逻辑,原理希望对你有帮助!

以上就是关于Jquery datatables导出Excle表的问题全部的内容,包括:Jquery datatables导出Excle表的问题、用JQuery导出HTML table到Excel,不能导出table中input值、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9451720.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存