javascript - 使用java脚本导出html表格excel。当找到 '#' 字符时不导出

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

最佳答案

感谢您的建议。我正在使用 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();
});
});

关于javascript - 使用java脚本导出html表格excel。当找到 '#' 字符时不导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27372288/

相关文章:

excel - 从 MS EXCEL 中无任何条件地从顶部或末尾删除行

javascript - 变换几何形状,保持对称性和边界尺寸不变

javascript - 点击打开/关闭 Jquery 菜单

javascript - 我可以从给定的选择框文本中获取选择框值吗

css - div在ie中不可见,在ff和chrome中不可见

Javascript 下一张和上一张图像

c# - 如何让用户在 C# 中选择文件位置

javascript - AngularJS:错误:[$injector:unpr] 未知提供者:$scopeProvider <- $scope <- productService

javascript - 使用 HTML 和/或 Javascript 自包含登录

excel - 使用 Excel 将信息提交到网站表单并解析结果