gpt4 book ai didi

php - 导出到 Excel 时排除最后一列

转载 作者:搜寻专家 更新时间:2023-10-31 21:05:00 25 4
gpt4 key购买 nike

如何在使用jquery 导出为ex​​cel 格式时排除数据表的最后一列?我试过 .not("#tableid").removeexclude:"#tableid" 但没有用。有什么解决办法吗???下面是我的代码

表格代码:

<table class="table table-bordered table-striped tableToExcel" id="tbl_datatable">
<thead>
<tr>
<th>Identifier</th>
<th>Group Name</th>
<th>Group Name English</th>
<th>Fiscal Year</th>
<th>Date</th>
<th>District</th>
<th>District in English</th>
<th>VDC</th>
<th>VDC in English</th>
<th>Ward No</th>
<th>Program</th>
<th>Livestock/Crop</th>
<th id="noExl">Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

J查询代码:

$(document).ready(function() {
$("#exportExcel").click(function() {
$(function() {
$(".tableToExcel").remove("#noExl").table2excel({
exclude: "#noExl",
name: "Excel Document Name",
filename: "Group",
exclude_img: true,
exclude_links: true,
exclude_inputs: false
});
});
});
});

最佳答案

是的,这可以很快完成。因为我们想排除整个列,所以我认为使用排除类作为 .noExl 是个坏主意。相反,我引入了一个新选项 columns :

defaults = {
exclude: ".noExl",
name: "Table2Excel",
columns: []
};

它指定应将哪些列导出到 Excel。美妙之处在于,通过这样做,您还可以指定列的顺序。然后我将 init() 函数循环更改为:

$(e.element).each(function(i, o) {
var tempRows = "";
$(o).find("tr").not(e.settings.exclude).each(function(i, o) {
if (e.settings.columns.length == 0) {
tempRows += "<tr>" + $(o).html() + "</tr>";
} else {
var row = "";
e.settings.columns.forEach(function(colIndex) {
//is it a thead or tbody row?
if ($(o).find('th').length > 0) {
row += $(o).find('th:eq(' + colIndex + ')')[0].outerHTML;
} else {
row += $(o).find('td:eq(' + colIndex + ')')[0].outerHTML;
}
})
tempRows += '<tr>' + row + '</tr>';
}
});
e.tableRows.push(tempRows);
});

它在 github 上 -> https://github.com/davidkonrad/table2excel

有些问题可以优化,但至少它有效。现在您可以准确指定要导出的列以及导出的顺序:

//export only column #2 and #3 and in reverse order
//NB! columns[] is zero based
$(".table2excel").table2excel({
name: "Excel Document Name",
filename: "myFileName",
exclude_img: true,
exclude_links: true,
exclude_inputs: true,
columns : [2,1]
})

演示 -> http://jsfiddle.net/qyrbazur/

在您的情况下,如果您想排除最后一列,请使用

$(".tableToExcel").remove("#noExl").table2excel({
name: "Excel Document Name",
filename: "Group",
exclude_img: true,
exclude_links: true,
exclude_inputs: false
columns : [0,1,2,3,4,5,6,7,8,9,10,11]
});

关于php - 导出到 Excel 时排除最后一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34058062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com