简体   繁体   中英

Can't export HTML table to Excel when using attributes on the table elements

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/

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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM