使用jquery导出为excel格式时如何排除数据表的最后一列?我试过 .not("#tableid")
、 .remove
、 exclude:"#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>
查询代码:
$(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/