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>

Script:

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

But excel is turning all numbers that are written like that "2.5" to Dates. Is there a way to stop that or do I have to write all number like that "2,5".

2 Answers2

1

I found the answer on scunliffe's post here: Format HTML table cell so that Excel formats as text? As per that post, you can apply this special CSS property to the table cell and Excel will start treating the value as a string instead of formatting it as number (hopefully this works the same for your Dates):

mso-number-format:"\@"; /*force text*/
Community
  • 1
  • 1
Elijah Lofgren
  • 1,437
  • 3
  • 23
  • 39
1

I know this is a old question but i will add my answer as it can help to other users that try to use numbers in excel.

In my case mso-number-format:"\@"; does not work so i use vnd.ms-excel.numberformat:0.0;

Example:

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

or

<th style="vnd.ms-excel.numberformat:0.0;">2,5</th>
Ingus
  • 1,026
  • 12
  • 34