Is it possible to improve the export of html table to xls

I used the JavaScript function from this question and tried to adapt it to my application. It works, but it can also be improved and I hope you can help me do it.

This is the function

function exportExcelReport(tblId) {
    var tab_text = "<table border='2px'><tr>";
    var table = document.getElementById(tblId);

    var style;
    for (var j = 0; j < table.rows.length; j++) {
        style = table.rows[j].className.split(" ");
        if (style.length < 2)
        tab_text = tab_text + table.rows[j].innerHTML + "</tr>";
    }

    tab_text = tab_text + "</table>";
    tab_text = tab_text.replace(/<a[^>]*>|<\/a>/g, "");
    tab_text = tab_text.replace(/<img[^>]*>/gi, "");
    tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, "");

    return window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
}

      

This is what the table looks like Table html

This is what I get after export enter image description here

As you can see, the exported excel file does not have a grid in the background, which actually looks strange. Do you have any idea why this is happening?

Also I would like to remove the last column following the YTD. Somehow it is possible to adjust the tab_text.replace (...) in the abowe code so that it can be ignored on export.

The column looks like this: html

</td><td width='20px'>
    <a class='infobox' href=''> 
         <img src='img/info.jpg' alt='info' width='18' height='18'>
         <span> 
             Service Engineer: ... <br>
             Datasource: ...
         </span>
    </a>
</tr>

      

thanks in advance!

+3


source to share


1 answer


yes we can do it with JQuery.

Example:



http://www.jqueryscript.net/demo/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel/

0


source







All Articles