HTML to Excel: How can tell Excel to treat columns as numbers?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 11:36:20

If you add a CSS Class to your page:

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

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

Update: Additional formatting options from @Aaron.

Here is a list of Excel formats: Styling Excel cells with mso-number-format

The best trick is to mock up an Excel spreadsheet with all the colors/(conditional) formats/formulas you require.

Keep it as simple as possible, just the bare minimum rows etc.

Then save the Excel spreadsheet "As Xml Spreadsheet"

Then you have a template to build a spreadsheet via Xml.

I generally clean up styles in the original Xml saved from the Excel workbook. Removing duplicates and renaming them.

Then it is just a matter of populating with whatever datasource you have.

I do not know what environment you are coding to, but in VB.NET with LINQ and Xml Literals, this is a very simple task.

It doesn't works sometimes if you add it using a css class in that case try it using in Style to your TD. I did it for formatting a column to a text. like this

 style="mso-number-format:\@;"

For you it would be as

  mso-number-format:General

I couldn't get any of the answers on this page to work in Excel 2010. I ended up using this.

<td x:num>123</td>

UPDATE: You also need to remove non-breaking spaces (nbsp's) from td cells containing numeric or money information to make formulas work on these cells in Excel. So the problem is solved.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!