当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Is it possible to improve html table export to xls

问题描述:

I have used the JavaScript function from this question and tried to adapt it to my app. It works but it also could be improved and I hope, you will help me to 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 how the table looks like

This is what I get as a result after export

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

Also I would like to remove the last column, that one after YTD. Is it somehow possible to adjust tab_text.replace(...) in the code abowe, so it that it could be ignored while exporting.

The column looks like this in 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>

Thx in advance!

网友答案:

yes we can do this by Jquery.

Example:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: