导出到Excel - 文本之间需要空格

时间:2015-02-19 15:59:41

标签: jquery html export-to-excel

我有一个HTML表格和一个导出到Excel的按钮。导出工作正常 - 表中的每个数据都被带入Excel,但在Excel中,当我使用Chrome或Firefox查看页面并导出时,文本之间没有空格!

这是HTML代码

<table>
<tr><td>Job Description</td></tr>
<tr><td><xsl:value-of select="job_desc" /></td></tr>
</table>

这是它在浏览器中的显示方式

Job Description
The process of writing a job description requires having a clear...

以下是导出后在Excel中的外观

JobDescription
Theprocessofwritingajobdescriptionrequireshavingaclear...

然后,我尝试在我的硬代码中获得一个空格:Job&#160;Description。它有效,让我有一个空格:Job Description

问题是:如何为动态数据获取空间?

这与我用来将数据导出到Excel的Jquery语法有关吗?

JS导出

$(".exportExcel").click(function(e) {
                    e.stopPropagation();
                    var table = $("." + $(this).data('target'));
                    window.open('data:application/vnd.ms-excel,' + $(table).html());
                    e.preventDefault();
                    });

2 个答案:

答案 0 :(得分:2)

我刚刚改变了

$(".exportExcel").click(function(e) {
                    e.stopPropagation();
                    var table = $("." + $(this).data('target'));
                    window.open('data:application/vnd.ms-excel,' + $(table).html());
                    e.preventDefault();
                    });

$(".exportExcel").click(function(e) {
                    e.stopPropagation();
                    var table = $("." + $(this).data('target'));
                    window.open('data:application/vnd.ms-excel,' +  encodeURIComponent(table[0].outerHTML));
                    e.preventDefault();
                    });

我在两个单词之间有空格。

答案 1 :(得分:0)

您可以尝试使用 pre 标记包装表格 - 保留格式:

&#13;
&#13;
<pre>
    <table>
        <tr><td>Job Description</td></tr>
        <tr><td><xsl:value-of select="job_desc" /></td></tr>
     </table>
 </pre>
&#13;
&#13;
&#13;