datatables导出到excel <select option =“”>

时间:2016-06-14 16:25:33

标签: javascript html excel html5 datatable

在我的测试页面中,我插入了一个选项列表,我需要只导出excel所选的值,但现在我的excel结果包括&#34;选择选项&#34;的所有列表。 我的代码: &#13; &#13; &lt;!DOCTYPE html&gt;&#13; &LT; HTML&GT;&#13;     &LT; HEAD&GT;&#13;         &lt; title&gt;测试导出Excel&#34; SELECT OPTION&#34; &LT; /标题&GT;&#13;         &lt; meta charset =&#34; UTF-8&#34;&gt;&#13;         &lt; meta name =&#34; viewport&#34; content =&#34; width = device-width,initial-scale = 1.0&#34;&gt;&#13;     &LT; /头&GT;&#13;     &LT;身体GT; &#13;     &lt; script src =&#34; https://code.jquery.com/jquery-1.12.3.js"&gt;&lt; / script&gt;&#13;     &lt; script src =&#34; https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"&gt;&lt; / script&gt;&#13;     &lt; script src =&#34; https://cdn.datatables.net/buttons/1.2.1/js/dataTables.buttons.min.js"&gt;&lt; / script&gt;&#13;     &lt; script src =&#34; https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"&gt;&lt; / script&gt;&#13;     &lt; script src =&#34; https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"&gt;&lt; / script&gt;&#13;     &lt; script src =&#34; https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"&gt;&lt; / script&gt;&#13;     &lt; script src =&#34; https://cdn.datatables.net/buttons/1.2.1/js/buttons.html5.min.js"&gt;&lt; / script&gt;&#13;     &#13;     &lt; link href =&#34; https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css"的rel =&#34;样式表&#34;类型=&#34;文本/ CSS&#34;&GT;&#13;     &lt; link href =&#34; https://cdn.datatables.net/buttons/1.2.1/css/buttons.dataTables.min.css"的rel =&#34;样式表&#34;类型=&#34;文本/ CSS&#34;&GT;&#13;     &LT;脚本&GT;&#13;     &#13;     $(document).ready(function(){&#13;     $(&#39; #example&#39;)。DataTable({&#13;         dom:&#39; Bfrtip&#39;,&#13;         按钮:[&#13;             &#39; copy&#39;,&#39; csv&#39;,&#39; excel&#39;,&#39; pdf&#39;&#13;         ]&#13;     });&#13; });&#13;     &#13;     &LT; /脚本&GT;&#13;     &#13;         &LT; DIV&GT;&#13;      &lt; table id =&#34;示例&#34;类=&#34;显示&#34; CELLSPACING =&#34; 0&#34;边界=&#34; 1&#34;宽度=&#34; 100%&#34;&GT;&#13;         &LT; THEAD&GT;&#13;             &LT; TR&GT;&#13;                 &LT的第i;姓名&LT; /第&GT;&#13;                 &LT的第i;位置&LT; /第&GT;&#13;                 &LT的第i;办公和LT; /第&GT;&#13;                 &LT的第i;年龄&LT; /第&GT;&#13;                 &lt; th&gt;开始日期&lt; / th&gt;&#13;                 &LT的第i;薪酬和LT; /第&GT;&#13;             &LT; / TR&GT;&#13;         &LT; / THEAD&GT;&#13;         &LT; TBODY&GT;&#13;             &LT; TR&GT;&#13;                 &lt; td&gt; Tiger Nixon&lt; / td&gt;&#13;                 &lt; td&gt;系统架构师&lt; / td&gt;&#13;                 &lt; td&gt;&lt; select class =&#34; form-control&#34;&gt;&#13;                         &lt; option value =&#34; Edinburgh&#34;选择&GT;爱丁堡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Singapore&#34; &GT;新加坡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Tokyo&#34; &gt;东京&lt; /选项&gt;&#13;                     &LT; /选择&GT; &#13;                 &LT; / TD&GT;&#13;                 &LT; TD&GT; 61℃; / TD&GT;&#13;                 &LT; TD&GT; 2011/04/25℃; / TD&GT;&#13;                 &LT; TD&GT; $三二〇八〇〇&LT; / TD&GT;&#13;             &LT; / TR&GT;&#13;             &LT; TR&GT;&#13;                 &lt; td&gt; Garrett Winters&lt; / td&gt;&#13;                 &LT; TD&GT;会计&LT; / TD&GT;&#13;                 &lt; td&gt;&lt; select class =&#34; form-control&#34;&gt;&#13;                         &lt; option value =&#34; Edinburgh&#34; &GT;爱丁堡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Singapore&#34; &GT;新加坡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Tokyo&#34;选择&gt;东京&lt; /选项&gt;&#13;                     &LT; /选择&GT; &#13;                 &LT; / TD&GT;&#13;                 &LT; TD&GT; 63&LT; / TD&GT;&#13;                 &LT; TD&GT; 2011/07/25℃; / TD&GT;&#13;                 &LT; TD&GT; $十七万〇七百五十&LT; / TD&GT;&#13;             &LT; / TR&GT;&#13;             &LT; TR&GT;&#13;                 &lt; td&gt; Ashton Cox&lt; / td&gt;&#13;                 &lt; td&gt;初级技术作者&lt; / td&gt;&#13;                 &lt; td&gt;&lt; select class =&#34; form-control&#34;&gt;&#13;                         &lt; option value =&#34; Edinburgh&#34; &GT;爱丁堡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Singapore&#34; &GT;新加坡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Tokyo&#34;选择&gt;东京&lt; /选项&gt;&#13;                     &LT; /选择&GT; &#13;                 &LT; / TD&GT;&#13;                 &LT; TD&GT; 66&LT; / TD&GT;&#13;                 &LT; TD&GT; 2009/01/12 LT; / TD&GT;&#13;                 &LT; TD&GT; $ 86,000&LT; / TD&GT;&#13;             &LT; / TR&GT;&#13;                         &LT; TR&GT;&#13;                 &lt; td&gt; Shad Decker&lt; / td&gt;&#13;                 &lt; td&gt;区域主任&lt; / td&gt;&#13;                 &lt; td&gt;&lt; select class =&#34; form-control&#34;&gt;&#13;                         &lt; option value =&#34; Edinburgh&#34; &GT;爱丁堡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Singapore&#34;选择&GT;新加坡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Tokyo&#34; &gt;东京&lt; /选项&gt;&#13;                     &LT; /选择&GT; &#13;                 &LT; / TD&GT;&#13;                 &LT; TD&GT; 51&LT; / TD&GT;&#13;                 &LT; TD&GT;一十一分之二千零八/ 13&LT; / TD&GT;&#13;                 &LT; TD&GT; $ 183,000&LT; / TD&GT;&#13;             &LT; / TR&GT;&#13;             &LT; TR&GT;&#13;                 &lt; td&gt; Michael Bruce&lt; / td&gt;&#13;                 &lt; td&gt; Javascript Developer&lt; / td&gt;&#13;                 &lt; td&gt;&lt; select class =&#34; form-control&#34;&gt;&#13;                         &lt; option value =&#34; Edinburgh&#34;选择&GT;爱丁堡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Singapore&#34; &GT;新加坡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Tokyo&#34; &gt;东京&lt; /选项&gt;&#13;                     &LT; /选择&GT; &#13;                 &LT; / TD&GT;&#13;                 &LT; TD&GT; 29&LT; / TD&GT;&#13;                 &LT; TD&GT; 2011/06/27 LT; / TD&GT;&#13;                 &LT; TD&GT; $ 183,000&LT; / TD&GT;&#13;             &LT; / TR&GT;&#13;             &LT; TR&GT;&#13;                 &lt; td&gt; Donna Snider&lt; / td&gt;&#13;                 &lt; td&gt;客户支持&lt; / td&gt;&#13;                 &lt; td&gt;&lt; select class =&#34; form-control&#34;&gt;&#13;                         &lt; option value =&#34; Edinburgh&#34; &GT;爱丁堡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Singapore&#34;选择&GT;新加坡&LT; /选项&GT;&#13;                         &lt; option value =&#34; Tokyo&#34; &gt;东京&lt; /选项&gt;&#13;                     &LT; /选择&GT; &#13;                 &LT; / TD&GT;&#13;                 &LT; TD&GT; 27&LT; / TD&GT;&#13;                 &LT; TD&GT; 2011/01/25℃; / TD&GT;&#13;                 &LT; TD&GT; $ 112,000&LT; / TD&GT;&#13;             &LT; / TR&GT;&#13;         &LT; / tbody的&GT;&#13;      &LT; /表&gt;&#13;             &#13;         &LT; / DIV&GT;&#13;     &LT; /体&GT;&#13; &LT; / HTML&GT;&#13; &#13; &#13; 在excel中导出的结果是可以的,除了&#34; office&#34;列(&#34; C&#34;)包含选项列表(爱丁堡,新加坡,东京)中的所有值。 导出excel结果

2 个答案:

答案 0 :(得分:2)

您需要定义exportOptions: 这是给你的代码

var buttonCommon = { exportOptions: { format: { body: function(data, column, row, node) {if (column == 2) { return $(data).find("option:selected").text() } else return data } } } };

然后在定义按钮时,使用以下按钮: buttons: [ 'copy', $.extend(true, {}, buttonCommon, { extend: "csv" }),$.extend(true, {}, buttonCommon, { extend: "excel" }), $.extend(true, {}, buttonCommon, { extend: "pdf" })]

<!DOCTYPE html>
<html>
    <head>
        <title>Test Export Excel "SELECT OPTION" </title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>     
    <script src="https://code.jquery.com/jquery-1.12.3.js"></script>
    <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.2.1/js/dataTables.buttons.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
    <script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
    <script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.html5.min.js"></script>
    
    <link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css">
    <link href="https://cdn.datatables.net/buttons/1.2.1/css/buttons.dataTables.min.css" rel="stylesheet" type="text/css">
    <script>
    var buttonCommon = {
            exportOptions: {
                format: {
                    body: function (data, column, row, node) {
                        // if it is select
                        if (column == 2) {
                            return $(data).find("option:selected").text()
                        } else return data
                    }
                }
            }
        };
    $(document).ready(function() {
    $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
                'copy', $.extend(true, {}, buttonCommon, {
                    extend: "csv"
                }), $.extend(true, {}, buttonCommon, {
                    extend: "excel"
                }), $.extend(true, {}, buttonCommon, {
                    extend: "pdf"
                })
            ]
    } );
} );
    
    </script>
    
        <div>
     <table id="example" class="display" cellspacing="0" border="1" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td><select class="form-control">
                        <option value="Edinburgh" selected>Edinburgh</option>
                        <option value="Singapore"         >Singapore</option>
                        <option value="Tokyo"             >Tokyo    </option>
                    </select>    
                </td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td><select class="form-control">
                        <option value="Edinburgh"     >Edinburgh</option>
                        <option value="Singapore"     >Singapore</option>
                        <option value="Tokyo" selected>Tokyo    </option>
                    </select>    
                </td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
            <tr>
                <td>Ashton Cox</td>
                <td>Junior Technical Author</td>
                <td><select class="form-control">
                        <option value="Edinburgh"     >Edinburgh</option>
                        <option value="Singapore"     >Singapore</option>
                        <option value="Tokyo" selected>Tokyo    </option>
                    </select>    
                </td>
                <td>66</td>
                <td>2009/01/12</td>
                <td>$86,000</td>
            </tr>
                        <tr>
                <td>Shad Decker</td>
                <td>Regional Director</td>
                <td><select class="form-control">
                        <option value="Edinburgh"         >Edinburgh</option>
                        <option value="Singapore" selected>Singapore</option>
                        <option value="Tokyo"             >Tokyo    </option>
                    </select>    
                </td>
                <td>51</td>
                <td>2008/11/13</td>
                <td>$183,000</td>
            </tr>
            <tr>
                <td>Michael Bruce</td>
                <td>Javascript Developer</td>
                <td><select class="form-control">
                        <option value="Edinburgh" selected>Edinburgh</option>
                        <option value="Singapore"         >Singapore</option>
                        <option value="Tokyo"             >Tokyo    </option>
                    </select>    
                </td>
                <td>29</td>
                <td>2011/06/27</td>
                <td>$183,000</td>
            </tr>
            <tr>
                <td>Donna Snider</td>
                <td>Customer Support</td>
                <td><select class="form-control">
                        <option value="Edinburgh"         >Edinburgh</option>
                        <option value="Singapore" selected>Singapore</option>
                        <option value="Tokyo"             >Tokyo    </option>
                    </select>    
                </td>
                <td>27</td>
                <td>2011/01/25</td>
                <td>$112,000</td>
            </tr>
        </tbody>
     </table>
            
        </div>
    </body>
</html>

答案 1 :(得分:0)

我身边的另一种解决方案。 我通过在表的末尾创建一个重复的列(但使用文本而不是下拉按钮)并使用不显示将其隐藏来解决了这个问题。

使用jQuery使用另一列中的选定下拉数据更新隐藏列。然后我使用

隐藏了导出的下拉列

exportOptions:{列:列:[0、1、3、4、5、6]}

(第2列具有下拉菜单)

<!DOCTYPE html>
<html>
<head>
<title>Test Export Excel "SELECT OPTION" </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>     
<script src="https://code.jquery.com/jquery-1.12.3.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.1/js/buttons.html5.min.js"></script>

<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css">
<link href="https://cdn.datatables.net/buttons/1.2.1/css/buttons.dataTables.min.css" rel="stylesheet" type="text/css">
<script>
$(document).ready(function() {
$('#example').DataTable( {
dom: 'Bfrtip',
buttons: [
    {
        extend: 'excel',
        text: 'Excel',
        exportOptions: {
             columns: [ 0, 1, 3, 4, 5, 6]
        }
    }
],
select: true
} );
} );
</script>

<div>
<table id="example" class="display" cellspacing="0" border="1" width="100%">
<thead>
    <tr>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th style="display: none;">Office</th>
        <th>Age</th>
        <th>Start date</th>
        <th>Salary</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Tiger Nixon</td>
        <td>System Architect</td>
        <td><select class="form-control">
                <option value="Edinburgh" selected>Edinburgh</option>
                <option value="Singapore">Singapore</option>
                <option value="Tokyo">Tokyo    </option>
            </select>    
        </td>
        <td style="display: none;">Edinburgh</td>
        <td>61</td>
        <td>2011/04/25</td>
        <td>$320,800</td>
    </tr>
    <tr>
        <td>Garrett Winters</td>
        <td>Accountant</td>
        <td><select class="form-control">
                <option value="Edinburgh">Edinburgh</option>
                <option value="Singapore">Singapore</option>
                <option value="Tokyo" selected>Tokyo    </option>
            </select>    
        </td>
        <td style="display: none;">Tokyo</td>
        <td>63</td>
        <td>2011/07/25</td>
        <td>$170,750</td>
    </tr>
    <tr>
        <td>Ashton Cox</td>
        <td>Junior Technical Author</td>
        <td><select class="form-control">
                <option value="Edinburgh">Edinburgh</option>
                <option value="Singapore">Singapore</option>
                <option value="Tokyo" selected>Tokyo</option>
            </select>    
        </td>
        <td style="display: none;">Tokyo</td>
        <td>66</td>
        <td>2009/01/12</td>
        <td>$86,000</td>
    </tr>
                <tr>
        <td>Shad Decker</td>
        <td>Regional Director</td>
        <td><select class="form-control">
                <option value="Edinburgh">Edinburgh</option>
                <option value="Singapore" selected>Singapore</option>
                <option value="Tokyo">Tokyo    </option>
            </select>    
        </td>
        <td style="display: none;">Singapore</td>
        <td>51</td>
        <td>2008/11/13</td>
        <td>$183,000</td>
    </tr>
    <tr>
        <td>Michael Bruce</td>
        <td>Javascript Developer</td>
        <td><select class="form-control">
                <option value="Edinburgh" selected>Edinburgh</option>
                <option value="Singapore">Singapore</option>
                <option value="Tokyo">Tokyo    </option>
            </select>    
        </td>
        <td style="display: none;">Edinburgh</td>
        <td>29</td>
        <td>2011/06/27</td>
        <td>$183,000</td>
    </tr>
    <tr>
        <td>Donna Snider</td>
        <td>Customer Support</td>
        <td><select class="form-control">
                <option value="Edinburgh">Edinburgh</option>
                <option value="Singapore" selected>Singapore</option>
                <option value="Tokyo">Tokyo    </option>
            </select>    
        </td>
        <td style="display: none;">Singapore</td>
        <td>27</td>
        <td>2011/01/25</td>
        <td>$112,000</td>
    </tr>
</tbody>
</table>
    
</div>
</body>
</html>