【问题标题】:Can't export HTML table to Excel when using attributes on the table elements在表格元素上使用属性时无法将 HTML 表格导出到 Excel
【发布时间】:2012-12-06 02:45:01
【问题描述】:

我正在尝试使用我在互联网上找到的这个简单功能将 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/

【问题讨论】:

  • 我在打开文件时也面临同样的问题,它会生成错误,即文件的格式可能与指定的不同,即“xsl”,并且此生成的 excel 的网格线也不会消失.......

标签: jquery html excel html-table


【解决方案1】:

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

此代码适用于我:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多