文章最初发表于szhshp的第三边境研究所
转载请注明
Datatable Excel输出
这个方法对主流浏览器适用,特别是IE Edge
有个需求需要在Datatable输出的Excel顶端添加几行数据, 看了下Datatable官方的实现, 作者似乎也没啥好主意, 不过一些用户提供了方法。
基于Button.Customize
参数实现:
jQuery(document).ready(function($) { $('table#datatable').dataTable({ buttons: [{ extend: 'excelHtml5', render: function ( data, type, full, meta ) { return 'Download'; //change the button text here }, customize: function(xlsx) { var sheet = xlsx.xl.worksheets['sheet1.xml']; var numrows = 4; // add styles for the column header, these row will be moved down var clRow = $('row', sheet); $(clRow[0]).find('c').attr('s', 32); //update Row clRow.each(function () { var attr = $(this).attr('r'); var ind = parseInt(attr); ind = ind + numrows; $(this).attr("r", ind); }); // Create row before data $('row c ', sheet).each(function(index) { var attr = $(this).attr('r'); var pre = attr.substring(0, 1); var ind = parseInt(attr.substring(1, attr.length)); ind = ind + numrows; $(this).attr("r", pre + ind); }); function addRow(index, data) { var row = sheet.createElement('row'); row.setAttribute("r", index); for (i = 0; i