【问题标题】:export html table to Excel numbers became dates将 html 表格导出到 Excel 数字变成日期
【发布时间】:2016-10-30 18:28:09
【问题描述】:

我想将 html 表格导出到 Excel。

表:

<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”这样的所有数字。

【问题讨论】:

    标签: javascript html excel


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      我在 scunliffe 的帖子中找到了答案:Format HTML table cell so that Excel formats as text? 根据该帖子,您可以将此特殊的 CSS 属性应用于表格单元格,Excel 将开始将值视为字符串而不是将其格式化为数字(希望这对您的日期同样有效):

      mso-number-format:"\@"; /*force text*/
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2016-05-15
        • 2018-06-24
        • 2023-03-21
        • 1970-01-01
        • 2021-07-22
        • 2014-04-08
        相关资源
        最近更新 更多