将HTML表格导出到Excel,数字变成日期

3

I want to export a html table to Excel.

Table:

<div id="table">
<table>
    <thead>
        <tr>
            <th>Column</th>
            <th>Column</th>
        </tr>
    </thead>
      <tbody id="tabbodyExp">
        <tr>
            <th>2.5</th>
            <th>2.6</th>
        </tr>
    </tbody>
</table>
</div>

脚本:

$("#btnExport").click(function (e) {
    window.open('data:application/vnd.ms-excel,' + $('#table').html());
    e.preventDefault();
});

但 Excel 把那些写作“2.5”的数字都转换成了日期格式。有没有办法停止这种转换或者我必须把所有数字写成“2,5”的形式。

2个回答

1
我在scunliffe的帖子中找到了答案,链接在这里: 如何使HTML表格单元格在Excel中以文本格式显示? 根据该帖子,您可以将此特殊的CSS属性应用于表格单元格,Excel将开始将值视为字符串而不是将其格式化为数字(希望对于您的日期也是同样有效的):
mso-number-format:"\@"; /*force text*/

1

我知道这是一个老问题,但我会添加我的答案,因为它可以帮助其他试图在Excel中使用数字的用户。

在我的情况下,mso-number-format:"\@";不起作用,所以我使用vnd.ms-excel.numberformat:0.0;

例如:

<th style="vnd.ms-excel.numberformat:0.0;">2.5</th>

或者

<th style="vnd.ms-excel.numberformat:0.0;">2,5</th>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接