Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude last column while exporting to excel

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
            });
        });
    });
});
like image 586
Xravn Avatar asked Mar 22 '26 05:03

Xravn


1 Answers

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]
});
like image 89
davidkonrad Avatar answered Mar 23 '26 20:03

davidkonrad