2

that following code is working fine. but when i use <table width=100% id="tb"> like that, it wont work properly. why is that? it will export to excel but not in proper format. is there any way to get it correctly?

HTML:

<input type="button" id="btnExport" value=" Export Table data into Excel " />
<br/>
<br/>
<div id="dvData">
    <table>
        <tr>
            <th>Column One</th>
            <th>Column Two</th>
            <th>Column Three</th>
        </tr>
        <tr>
            <td>row1 Col1</td>
            <td>row1 Col2</td>
            <td>row1 Col3</td>
        </tr>
        <tr>
            <td>row2 Col1</td>
            <td>row2 Col2</td>
            <td>row2 Col3</td>
        </tr>
        <tr>
            <td>row3 Col1</td>
            <td>row3 Col2</td>
            <td><a href="http://www.jquery2dotnet.com/">http://www.jquery2dotnet.com/</a>
            </td>
        </tr>
    </table>
</div>

JS:

$("#btnExport").click(function (e) {
   window.open('data:application/vnd.ms-excel,' + $('#dvData').html());
   e.preventDefault();
});
7
  • I checked your code and it is working at my end. Please check the php version at your end. In which browser are you testing this?
    – Ashish
    Mar 15, 2016 at 7:03
  • @Ashish Chrome and PHP ver : 5.3 Mar 15, 2016 at 7:15
  • @Ashish i mean when i use <table> its working fine. bt if i use <table width=100% id="tb"> it will export to excel/ but not it proper format Mar 15, 2016 at 7:16
  • @Ashish try this code by adding <table id="tb"> this and without adding this jsfiddle.net/lesson8/jWAJ7/light Mar 15, 2016 at 7:19
  • 1
    The data URI needs to be URI encoded. window.open('data:application/vnd.ms-excel,' + encodeURI($('#dvData').html())); Mar 15, 2016 at 7:48

2 Answers 2

2

Try this:

window.open('data:application/vnd.ms-excel,' +  encodeURIComponent($('#dvData').html()));

Hope it helps.

1
  • Thank you very much. its working fine. but it wont show the excel cells. why is that? :) Mar 15, 2016 at 8:02
2

Source :Export HTML table to excel - using jQuery or Java

I would recommend Apache POI, we've been using it for years, never had any problems.

Alot of examples online to get a good start, and the documentation on the site is also good: http://poi.apache.org/spreadsheet/quick-guide.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.