将Bootstrap数据表的所有行导出到Excel [英] Export Bootstrap Data table's all rows to Excel

查看:388
本文介绍了将Bootstrap数据表的所有行导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Bootstrap数据表行导出到Excel时遇到问题.

I am facing an issue on exporting Bootstrap Data Table Rows to Excel.

为了将数据导出到Excel,我正在使用一个名为 jquery.table2excel.js 的外部插件.

For Exporting Data to Excel I am using an external plugin called jquery.table2excel.js.

将数据表导出到excel的代码如下:

Code for Exporting Data table to excel as below:

<script type="text/javascript" src="js/jquery.table2excel.js">
</script>
<script>
 $(function() {
    var startDate = $(".startDate").val();
    var endDate = $(".endDate").val();
    $("#exportExcel").click(function(){
        $("#table_id").table2excel({
            exclude: ".noExl",
            //name: "Excel Document Name",
            filename:  "Data from " + startDate + " to " + endDate
        }); 
     });
    $("#table_id").dataTable();
  });
</script>

对于数据表,我正在使用以下库:

For Datatable I am using below library:

<script type="text/javascript" src="js/jquery.dataTables.min.js">
</script>
<script type="text/javascript" src="js/dataTables.bootstrap.js">
</script>

表如下:

<table id="table_id" class="table table-striped table-condensed table-
bordered">
  <thead>`Table Headers here`</thead>
  <tbody>`Rows from Database here`</tbody>
</table>

问题描述如下:

  1. 当我尝试使用导出"功能时,只有可见行被导出到excel中,而不是分页的行.

例如假设如果我每页有10行,那么仅前10行将被导出,而当我将每行的行数更改为25时,所有25行都将导出.

e.g. Suppose if I have 10 rows per page then only first 10 rows will be exported and when I change per page rows to 25 then all 25 gets exported.

我希望使用我正在使用的插件一次导出所有行.有什么想法吗?

I want all rows to be exported at once with plugin I am using. Any ideas please?

推荐答案

解决方案

您可以使用 $() 方法来访问所有内容甚至在DOM中都不存在的行,并使用这些行构造一个新表.然后,您可以在新构造的表上执行table2excel()以获得包含所有行的Excel文件.

SOLUTION

You can use $() method to get access to all rows even not present in DOM and construct a new table using these rows. Then you can execute table2excel() on a newly constructed table to get Excel file that contains all rows.

例如:

$(function() {
   var startDate = $(".startDate").val();
   var endDate = $(".endDate").val();

   $("#exportExcel").click(function(){
      $('<table>')
         .append(
            $("#table_id").DataTable().$('tr').clone()
         )
         .table2excel({
            exclude: ".excludeThisClass",
            name: "Worksheet Name",
            filename: "SomeFile" //do not include extension
         });
   });

   $("#table_id").dataTable();
});

演示

有关代码和演示,请参见此页面.

Excel 2013打开由table2excel.js生成的文件时,显示以下错误.

Excel 2013 displays the following error when opening the file produced by table2excel.js.

Excel无法打开文件[filename],因为文件格式或文件扩展名无效.确认文件未损坏,并且文件扩展名与文件格式匹配.

Excel cannot open the file [filename] because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

由于此错误,我宁愿使用 DataTables TableTools插件来代替它只能生成CSV文件,也可以使用Flash.

Because of this error, I would rather use DataTables TableTools plug-in instead even though it can only produce CSV files and also uses Flash.

这篇关于将Bootstrap数据表的所有行导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆