Reputation: 101
I need a JavaScript to export HTML tables to Excel. I have tried this script, it is exporting but when it found special character i.e '#' it stopping there it self not exporting further lines.
can anyone help me out,thanks in advance
<script src="/tpComment.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
\$(function() {
\$("#btnExport").click(function(e) {
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementById('dvData');
var table_html = table_div.outerHTML.replace(/ /g, '%20');
window.open(data_type + ', ' + table_html);
e.preventDefault();
//getting values of current time for generating the file name
});
});
</script>
<input type="button" onclick="CreateExcelSheet()" value="Create Excel Sheet">
<div id="dvData" >
<table >
<tr>
<th>name</th>
<th> address </th>
<th> no </th>
</tr><tr>
<td>ABC</td>
<td>#17 </td>
<td>99999</td>
</tr></table></div>
Upvotes: 0
Views: 2638
Reputation: 101
Thanks for the suggestions. I am using URL encoding.below script is working fine for special characters. My solution may helpful for others.
<script src="/tpComment.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function() {
$("#btnExport").click(function(e) {
window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#dvData').html()));
e.preventDefault();
});
});
Upvotes: 1