jquery - 将选定的行从表导出到Excel

标签 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>

请指导我添加以下代码:
  • 如果选择了所有行,则将整个表下载到excel。
  • 仅下载选定的行即可精益求精。
  • 最佳答案

    使用Grails的Export to Excel plugin。因此,一旦设置好,就可以在 Controller 中调用一个函数,该函数将自动将表中的所有行导出到excel。但是,由于只希望导出一些行,因此可以将行的ID发送回 Controller 功能,该功能可以仅基于选定的ID创建条件查询,然后自动将其导出到Excel。

    关于jquery - 将选定的行从表导出到Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24755144/

    相关文章:

    jquery - 在gsp中添加SubmitToRemote时“Ajax not found”

    grails - grails如何在请求参数中使用斜线(“/”)

    javascript - 国际统计短信字符

    javascript - Jquery 使两个函数一起运行

    javascript - 如何从第一页的按钮重新加载第二页?

    Java POI - 将工作簿转换为文件

    c# - 有没有使用 C# 将数组导出到 Excel 文件的快速方法?

    Excel 突出显示重复项并按颜色替代进行过滤

    hibernate - 如何使用Hibernate从Grails中的子实体访问父实体

    javascript - qTip2 中的 Ajax 表单