将html导出为ex​​cel,排除某些单元格或列

时间:2014-02-23 22:10:13

标签: jquery html excel export

   <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>

我使用上面的js导出到excel后用以下2行修改它以排除我表格中的单元格

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

但不起作用

1 个答案:

答案 0 :(得分:3)

我使用了这个插件:jQuery Plugin to Export HTML Tables

我只使用:

<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>

该插件有以下选项:

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

所以你可以忽略一些列。希望这可以帮助。