1

I'm trying to export an HTML table to Excel using this easy function that I found on the internet:

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

It works pretty well, but when I add an attribute to any elements in my table, like rowspan or colspan, the Excel does not export correctly.

Here's the HTML:

<div id="tabla_datos">
<table>
    <tr>
        <th>Columna 1</th>
        <th>Columna 2</th>
        <th>Columna 3</th>
    </tr>
    <tr>
        <td colspan='2'>Dato 1</td>
        <td>Dato2</td>
   </tr>
   <tr>
        <td>Dato 3</td>
        <td>dato 4</td>
        <td>Dato5</td>
   </tr>
</table>
</div>

<input type="button" id="btnExport" value=" Export Table data into Excel " />​

And here's the jsfiddle: http://jsfiddle.net/WAR2v/

1
  • I am facing same problem aslo while opening file it generates error that file might have different format than specified i.e "xsl" and no grid lines of this generated excel also disappears....... Commented Feb 20, 2013 at 9:52

1 Answer 1

1

A bit late, but did you try to encode your URI?

This code works for me:

$("#btnExport").click(function(e) {
    window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#tabla_datos').html()));
    e.preventDefault();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.