导出引导数据表所有行都为excel [英] Export Bootstrap Data table all rows to excel

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

问题描述

嗯!我将面向导出Bootstrap数据表行到Excel的问题。



对于将数据导出到Excel,我使用外部插件称为 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文件名,
文件名:数据从+ 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中,而不是分页的行。


$ b $例如假设如果我每页有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();
});




DEMO


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


注意


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


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


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

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

For Exporting Data to Excel i am using 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 as 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天全站免登陆