繁体   English   中英

在表格元素上使用属性时,无法将HTML表格导出到Excel

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

我正在尝试使用在互联网上找到的这个简单功能将HTML表导出到Excel:

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

它工作得很好,但是当我向表中的任何元素(例如rowspan或colspan)添加属性时,Excel均无法正确导出。

这是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 " />​

这是jsfiddle: http : //jsfiddle.net/WAR2v/

有点晚了,但是您是否尝试对URI进行编码?

该代码对我有用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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