从表中将选定的行导出到Excel

时间:2014-07-15 09:55:58

标签: jquery excel grails html-table

我在表格中有数据。每行都有一个复选框。当用户单击导出按钮时,我希望将选中的行导出到excel文件。

这是我的代码(GSP):

<div id="downloadExcelPopup" style="display: none">
    <div id="" class="newserial_popup1"
        style="width: 80%; height: 70%; float:left; ">
        <div class="newbrand">
            <div
                style="background: #ff9900; width: 100%; float: left; padding: 15px;">
                <h3 style="float: left; width: 80%; margin: 0px; color: #ffffff;">Export to Excel</h3>

                <button class="close md-close cancelDownload" aria-hidden="true"
                    data-dismiss="modal" type="button">×</button>

            </div>
            <div class="form-horizontal group-border-dashed"
                style="border-bottom: 1px solid #E5E5E5; margin-bottom: 10px;">
                <div class="modal-body form"
                    style="clear: both; padding: 15px 0px 0px 0px;">
<div class="content" >

                        <div class="table-responsive" id="clienttrievent">
                            <table class="table table-bordered" id="datatable3" >
                                    <thead>
                                    <tr>
                                        <th><g:checkBox name="myCheckbox" id="selectall" value="${false}" /></th>
                                        <th>Company Name</th>
                                        <th>ROC No</th>
                                        <th>Contact Address</th>
                                        <th>Contact No</th>

                                        <th>E-Mail</th>
                                        <th>Status</th>
                                    </tr>
                                </thead>
                                <tbody id="clienttable">
                                    <g:each in="${clientyeardetailsInstanceList}" status="i" var="customerInstance">
                                            <tr class="${(i % 2) == 0 ? 'even gradeC' : 'odd gradeX'}">

                                                <td><g:checkBox name="myCheckbox1" class="case" value="${false}" /></td>
<td>${customerInstance?.client?.companyname}</td>
<td>${customerInstance?.client?.registerno}</td>
<td>${customerInstance?.client?.companyaddress}</td>
<td>${customerInstance?.client?.contactno}</td>
<td>${customerInstance?.client?.email}</td>
<td>${(customerInstance?.client?.clientstatus) == 'on' ? 'ACTIVE' : 'INACTIVE'}</td>
                                            </tr>
                                        </g:each>


                                </tbody>
                            </table>

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

        <div class="modal-footer">
            <button class="btn btn-default btn-flat md-close cancelDownload"
                data-dismiss="modal" type="button">Cancel</button>
    </div>
</div>
</div>
</g:form>

    <script>
    $("#selectall").click(function () {
        var checkAll = $("#selectall").prop('checked');
            if (checkAll) {
                $(".case").prop("checked", true);
            } else {
                $(".case").prop("checked", false);
            }
        });
    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>

请指导我添加以下代码:

  1. 如果选择了所有行,请将完整表格下载到Excel。

  2. 单独下载选定的行以获得优秀。

1 个答案:

答案 0 :(得分:1)

使用导出到Grails的Excel plugin。因此,一旦设置完毕,就可以调用控制器中的一个函数,该函数会自动将表中的所有行导出到excel。但是,由于您只需要导出一些行,因此可以将行的ID发送回控制器功能,该功能可以仅根据选定的ID创建条件查询,并自动导出到Excel。