将带有下拉列表的html表导出,使用jQuery进行优化

问题描述 投票:0回答:1

我有一个html表,其中包含一些包含下拉列表的单元格,我正在尝试使用table2excel jquery插件将其导出到Excel。该插件将导出所有单元格,但包含选择标记的单元格除外。我如何解决这个问题?

示例html:

<!DOCTYPE html>
<html>
<head>
    <script type="javascript">
        var tableToExcel = (function() {
        var uri = 'data:application/vnd.ms-excel;base64,'
        , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
        , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
        , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
        return function(table, name) {
        if (!table.nodeType) table = document.getElementById(table)     
        var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
        window.location.href = uri + base64(format(template, ctx))
        }
        })()    
    </script>
</head>
<body>

<input type="button" onclick="tableToExcel('testTable', 'W3C Example Table')" value="Export to Excel">

<table id="testTable" summary="Sample tabe to export to excel" rules="groups" frame="hsides" border="2">
    <thead valign="top">
    <tr>
        <th>Col1</th>
        <th>Col2</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>Value1</td>
        <td>
            <select>
                <option value="option1">Option 1</option>
                <option value="option2" selected="selected">Option 2</option>
            </select>
        </td>
    </tr>
    </tbody>
</table>

</body>
</html>
javascript jquery export-to-excel
1个回答
0
投票

您可以动态生成表格HTML,因此可以根据需要设置单元格的格式

© www.soinside.com 2019 - 2024. All rights reserved.