使用java脚本导出html表excel。当它找到'#' chracter

时间:2014-12-09 05:49:16

标签: javascript html excel

我需要一个JavaScript来将HTML表格导出到Excel。 我已经尝试过这个脚本,它正在导出但是当它找到特殊的字符,即'#'时就会自动停止它 没有出口更多的线。

任何人都可以帮助我,提前谢谢

<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>

1 个答案:

答案 0 :(得分:1)

感谢您的建议。我正在使用URL encoding.below脚本适用于特殊字符。 我的解决方案可能对其他人有帮助。

 <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();
});
});
相关问题