How to exclude the last column of datatable while exporting to excel format using jquery? I have tried .not("#tableid"), .remove, exclude:"#tableid" but doesn't work. Is there any solution for it???Below is my code
Table code:
<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>
Jquery code:
$(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
});
});
});
});
Yes, that can be done very quickly. Since we want to exclude entire columns I think using exclude classes as .noExl is a bad idea. Instead I introduce a new option, columns :
defaults = {
exclude: ".noExl",
name: "Table2Excel",
columns: []
};
Which specifies which columns that should be exported to Excel. The beauty is, that by doing so you can also specify the order of the columns. And then I changed the init() function loop to :
$(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);
});
It is here on github -> https://github.com/davidkonrad/table2excel
Some issues could be optimized, but at least it works. Now you can specify exactly what columns you want to export and in which order :
//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]
})
demo -> http://jsfiddle.net/qyrbazur/
In your case, if you want to exclude the last column, use
$(".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]
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With