Export HTML table to Excel file

前端 未结 6 1814
我在风中等你
我在风中等你 2021-01-14 15:14

I\'ve been looking into some jQuery plugins that are capable of doing this. I decided to use the one at http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreads

6条回答
  •  伪装坚强ぢ
    2021-01-14 15:33

    VERY IMPORTANT, make sure the columns and rows have width greater than 0, this becomes an issue especially when rows are moved from one table to another table, (I have faced it when I moved rows between two dataTables)

    if columns(th) or rows(td) width is 0, you will get the table downloaded as .xls file, but the columns gets hidden in the excel file,

    use css or jquery to set the width greater than 0

    css

     th,  td {
      width:px;
    }
    

    jquery

    $(" th").css("width","px"); 
    $(" td").css("width","px"); 
    
    $("").click(function(e) {
        var a = document.createElement('a');
        var dataType = 'data:application/vnd.ms-excel';
        var getTable = document.getElementById('');
        var readTable = getTable.outerHTML.replace(/ /g, '%20');
        a.href = dataType + ', ' + readTable;
        a.download = '.xls';
        a.click();
        e.preventDefault();
    });
    
    

提交回复
热议问题