jQuery 将 Bootstrap 数据表的所有行导出到 Excel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31445260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Export Bootstrap Data table's all rows to Excel
提问by Gags
I am facing an issue on exporting Bootstrap Data Table Rows to Excel.
我在将 Bootstrap 数据表行导出到 Excel 时遇到问题。
For Exporting Data to Excel I am using an external plugin called jquery.table2excel.js
.
为了将数据导出到 Excel,我使用了一个名为jquery.table2excel.js
.
Code for Exporting Data table to excel as below:
将数据表导出到excel的代码如下:
<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:
问题描述如下:
- When I am trying to use Export function then only Visible Rows gets exported into the excel not the paginated rows.
- 当我尝试使用导出功能时,只有可见行被导出到 Excel 中,而不是分页行。
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.
例如,假设如果我每页有 10 行,那么只会导出前 10 行,当我将每页行更改为 25 时,所有 25 行都会被导出。
I want all rows to be exported at once with plugin I am using. Any ideas please?
我希望使用我正在使用的插件一次导出所有行。请问有什么想法吗?
回答by Gyrocode.com
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.
您可以使用$()
method 访问所有行,甚至不存在于 DOM 中,并使用这些行构建一个新表。然后您可以table2excel()
在新构建的表上执行以获取包含所有行的 Excel 文件。
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 pagefor code and demonstration.
有关代码和演示,请参阅此页面。
NOTES
笔记
Excel 2013 displays the following error when opening the file produced by table2excel.js
.
Excel 2013 打开由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.
Excel 无法打开该文件,
[filename]
因为文件格式或文件扩展名无效。验证文件未损坏并且文件扩展名与文件格式匹配。
Because of this error, I would rather use DataTables TableTools plug-ininstead even though it can only produce CSV files and also uses Flash.
由于这个错误,我宁愿使用DataTables TableTools 插件,尽管它只能生成 CSV 文件并且还使用 Flash。