简体   繁体   中英

Export html table as an excel sheet excluding hidden elements

I am trying to export html table as an excel. This html table have some hidden properties and I want to remove them when exporting the table. So I tried below.

if ('undefined' !== typeof module) {

    module.exports = function initExport() {
        var data_type = 'data:application/vnd.ms-excel';;

        $('table *:hidden').attr('data-todelete', 'true');
        var clonedItem = $($('table').html());
        $('table *:hidden').removeAttr('data-todelete');
        clonedItem.find('*:hidden').remove();

        var a = document.createElement('a');
        a.href = data_type + ', ' + clonedItem.wrap("<div />").parent().html();
        a.download = 'exported_table_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls';
        a.click();

    }
}

The excel is exporting but it contains following.

<thead_ngcontent-scg-0=""></thead>

EDITED

I use

a.href = data_type + ', ' + clonedItem.html().replace(/ /g, '%20');

instead of

a.href = data_type + ', ' + clonedItem.html();

Then the excel cantains below and this is the HTML structure that I want to export.

        <thead _ngcontent-xbe-0="">
   <tr _ngcontent-xbe-0="" role="row">
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 9px;">#</th>
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 63px;">Departure Airport</th>
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 72px;">Destination Airport</th>
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 56px;">Modified Time</th>
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 69px;">Availability</th>
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 54px;">Duration</th>
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 63px;">Departure Date</th>
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 24px;">Pax</th>
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 33px;">Total Price</th>
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 48px;">Time(Hours)</th>
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 47px;">Expired Time</th>
      <th _ngcontent-xbe-0="" class="sorting_disabled" rowspan="1" colspan="1" style="width: 59px;">Last Possible Price Changed Time</th>
   </tr>
</thead>

<tbody _ngcontent-xbe-0="">

   <tr _ngcontent-xbe-0="">
      <td _ngcontent-xbe-0="">1</td>
      <td _ngcontent-xbe-0="">ASD</td>
      <td _ngcontent-xbe-0="">ABC</td>
      <td _ngcontent-xbe-0="">2017-06-05 12:34:44</td>
      <td _ngcontent-xbe-0="" style="text-align: center;">true</td>
      <td _ngcontent-xbe-0="" style="text-align: center;">13</td>
      <td _ngcontent-xbe-0="">2017-07-31</td>
      <td _ngcontent-xbe-0="">1_0_0</td>
      <td _ngcontent-xbe-0="" style="text-align: right;">143.01</td>
      <td _ngcontent-xbe-0="" style="text-align: right;">1.00</td>
      <td _ngcontent-xbe-0="" style="text-align: center;">2017-06-05 13:34:44</td>
      <td _ngcontent-xbe-0="" style="text-align: center;">2017-06-05 12:34:44</td>
   </tr>

   <!--data-->


</tbody>

Any suggestions are appreciated.

Thank You

I don't really get your data-todelete , you don't seem to use it.

Try using the .clone() JQuery function :

if (module != null) {

    module.exports = function initExport() {
        var data_type = 'data:application/vnd.ms-excel';;

        $('table *:hidden').attr('data-todelete', 'true');
        var clonedItem = $('table').clone();
        $('table *:hidden').removeAttr('data-todelete');
        $('[data-todelete]',clonedItem).remove();

        var a = document.createElement('a');
        a.href = data_type + ', ' + clonedItem.html();
        a.download = 'exported_table_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls';
        a.click();

    }
}

I tested my own function to save a table to an XLS file, and it works fine with your table structure : https://jsfiddle.net/0qt01rmw/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM