0

Below is my sample code for exporting the HTML table to excel :

<!DOCTYPE HTML>
<html>
    <head>
        <script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" >
        </script>
        <script>
            $(function(){
                $('button').click(function(){
                    var dvData = $('#dvData').html(); 
                    window.open('data:application/vnd.ms-excel,' + $('#dvData').html());
                })
            })
        </script>
    </head>
    <body>
        <button>
            Export To Excel
        </button>
        <div id="dvData">
            <table>
                <th> ID </th>
                <th> Name </th>
                <tr>
                    <td>"1.0"</td>
                    <td>string1</td>
                </tr>
                <tr>
                    <td>10</td>
                    <td>string2</td>
                </tr>
            </table>
        </div>
    </body>
</html>

This exports 1.0 as 1 in excel which is wrong. I tried adding single quotes but that appears in the webpage which I don't want.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Neha
  • 1
  • 1
  • 1
    Possible duplicate: http://stackoverflow.com/questions/25989238/exporting-html-table-with-correct-format-in-javascript – 1cgonza May 17 '16 at 06:48

1 Answers1

0

Use "mso-number-format" and define your data format

i.e.: "#,##0.0"

for more information you can check this ans.

Exporting HTML table with correct format in javascript

Community
  • 1
  • 1
Sid
  • 801
  • 8
  • 19
  • Should be mentioned that the `data:application/vnd.ms-excel,...` is a `URI`. So data in this `URI` must be URI encoded. `window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#dvData').html()));`. – Axel Richter May 17 '16 at 07:19
  • @Sid This works but now the problem is a number like 1.0 is appearing as 1.00 but rather I want these numbers to be exported as it is like a string – Neha May 17 '16 at 08:27
  • @AxelRichter yes I have used encodeURIComponent in the real code – Neha May 17 '16 at 08:28
  • @Neha i got your point now tell me how you are generating table rows, if dymanically? then you need to add format in "TD" having decimal value content on conditionally – Sid May 17 '16 at 08:28
  • There is also a `mso-number-format` for text: `1.0` – Axel Richter May 17 '16 at 08:31
  • @Alex Richter defining mso-number-format:'@' works .. Thanks much :) – Neha May 17 '16 at 08:42