繁体   English   中英

使用下拉列表导出 html 表,以使用 jquery 表现出色

[英]Exporting html table with dropdowns, to excel using jquery

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

示例 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>

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

 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: generateTable(table) }; window.location.href = uri + base64(format(template, ctx)); }; })(); function generateTable(table) { let innerHTML = ` <thead valign="top"> <tr> ${Array.from(table.querySelectorAll("thead th ")).map(function( columnHeader ) { return `<th>${columnHeader.innerText}</th>`; }).join('')} </tr> </thead> <tbody> ${Array.from(table.querySelectorAll("tbody tr ")).map( function(row) { return `<tr> ${Array.from(row.querySelectorAll("td")).map( function(cell) { let text = ""; Array.from(cell.childNodes).forEach(childNode => { switch (childNode.nodeType) { case 1: { if ( childNode.nodeName === "SELECT" || childNode.nodeName === "INPUT" ) { text = text + childNode.value; } break; } case 3: { text = text + childNode.textContent; break; } } }); return `<td>${text}</td>`; } ).join("")}</tr>`; } ).join("")} </tbody> `; console.log(innerHTML); return innerHTML; }
 <!DOCTYPE html> <html> <head> <script type="javascript"> </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>

暂无
暂无

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

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