使用 java 脚本导出 html table excel.找到“#”时不导出

Exporting html table excel using java script. not exporting when it find '#' chracter

本文关键字:找到 table java 脚本 html 使用 excel      更新时间:2023-09-26

我需要 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>

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

 <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();
});
});