Home » excel » jquery – export html to excel excluding some cells or columns

jquery – export html to excel excluding some cells or columns

Posted by: admin May 14, 2020 Leave a comment

Questions:
   <script>
    var tableToExcel = (function() {
      var uri = 'data:application/vnd.ms-excel;base64,'
        , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
        , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
        , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
      return function(table, name) {
        if (!table.nodeType){
 var exTable = $('#mywarning').clone();
 exTable.find('.exclude').remove();
} 
        var ctx = {worksheet: name || 'Worksheet', table: exTable.innerHTML}
        window.location.href = uri + base64(format(template, ctx))
      }
    })()
        </script>

i use the above js to export to excel after modifying it with the following 2 lines to exclude cells in my table

 var exTable = $('#mywarning').clone();
 exTable.find('.exclude').remove();

but does not work

How to&Answers:

i used this plugin: jQuery Plugin to Export HTML Tables

i only use:

<script src="jquery-tableexport/tableExport.js"></script>
<script src="jquery-tableexport/jquery.base64.js"></script>

HTML:

<a href="#" onClick ="$('#table-name').tableExport({type:'excel', separator:';', escape:'false'});" id="buttonExportData" class="ui-btn ui-btn-inline ui-mini ui-shadow ui-corner-all">Export XLS</a>

the plugin has the following options:

separator: ','
ignoreColumn: [2,3],
tableName:'yourTableName'
type:'csv'
pdfFontSize:14
pdfLeftMargin:20
escape:'true'
htmlContent:'false'
consoleLog:'false'

so you could ignore some columns. Hope this helps.