HTML 到 Excel:如何告诉 Excel 将列视为数字?

2022-01-12 00:00:00 excel numbers formatting html formulas

在 Excel 中打开 HTML 时需要实现以下目标 (Response.contentType="application/vnd.ms-excel") :

I need to achieve the following when opening an HTML in Excel (Response.contentType="application/vnd.ms-excel") :

  • 强制 Excel 将 td 单元格的内容视为数字
  • 进行上述操作,以便任何后续用户输入的公式都适用于这些单元格(打开电子表格时)

到目前为止,我成功地将 style="vnd.ms-excel.numberformat:0.00" 添加到有问题的 td 单元格中.当我在 Excel 中右键单击单元格时,单元格的内容正确显示为数字,但是公式不起作用.

So far I was successful with adding style="vnd.ms-excel.numberformat:0.00" to the td cells in question. The contents of the cells are correctly shown as numbers when I right click on them in the Excel, however the formulas don't work.

如果成功,该技术将非常有用,因为用户可以根据自定义要求使用适当的公式对任何 Web Excel 报表进行增强.提前致谢.

If successful, that technique would be quite useful because any web Excel report could be user enhanced with appropriate formulas according to custom requirements. Thanks in advance.

推荐答案

如果你给你的页面添加一个 CSS 类:

If you add a CSS Class to your page:

.num {
  mso-number-format:General;
}
.date {
  mso-number-format:"Short Date";
}

然后在你的 TD 上打这些课程,这行得通吗?

And slap those classes on your TD's, does it work?

<td class="num">34</td>
<td class="num">17.0</td>
<td class="date">12/17/2008</td> <!-- if you are playing with dates too -->

更新: 其他格式选项来自@Aaron.

相关文章