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

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

问题描述



将数据导出到Excel我正在使用一个名为 <$ c的外部插件$ C> jquery.table2excel.js 即可。



将数据表导出为ex​​cel的代码如下:

  ; script type =text / javascriptsrc =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文件名,
filename:数据从+ startDate +到+ endDate
});
});
$(#table_id)。 dataTable();
});
< / script>

对于Datatable我正在使用下面的库:

 < script type =text / javascriptsrc =js / jquery.dataTables.min.js> 
< / script>
< script type =text / javascriptsrc =js / dataTables.bootstrap.js>
< / script>

表格如下:

 < table id =table_idclass =table table-striped table-condensed table- 
bordered>
< thead>`表标题在这里< / thead>
< tbody>`来自Database here的行< / tbody>
< / table>

问题描述如下:


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

例如假设如果我每页10行,那么只有前10行将被导出,而当我将页面行更改为25时,所有25将被导出。



我想要所有行要立即使用我正在使用的插件导出。任何想法?

解决方案

解决方案



您可以使用 $() 方法即使不存在于DOM中也可以访问所有行,并使用这些行构造新表。然后,您可以在新构造的表上执行 table2excel()来获取包含所有行的Excel文件。



示例:

  $(function(){
var startDate = $(。startDate ;
var endDate = $(。endDate)。val();

$(#exportExcel)click(function(){
$(' ; table>')
.append(
$(#table_id)。DataTable()。$('tr')。clone()

.table2excel {
exclude:.excludeThisClass,
name:Worksheet Name,
filename:SomeFile//不包括扩展名
});
} );

$(#table_id)。dataTable();
});



演示



请参阅此页面代码和演示。



注意



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


Excel无法打开文件 [filename] ,因为文件格式或文件扩展名无效。验证文件没有损坏,文件扩展名与文件的格式相符。


由于这个错误,我会而是使用 DataTables TableTools插件,尽管它只能生成CSV文件,也可以使用Flash。


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

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

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 is as below:

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

The problem is described as below:

  1. When I am trying to use Export function then only Visible Rows gets exported into the excel not the paginated rows.

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?

解决方案

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.

For example:

$(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();
});

DEMO

See this page for code and demonstration.

NOTES

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

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.

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.

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

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