从表中导出选定的行到Excel

我有表中的数据。 每一行都有一个checkbox。 当用户点击导出button时,我想要检查的行被导出到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. 如果select了所有行,请将全表下载到excel。

  2. 单独下载选定的行来优化。

使用Grails的Export to Excel插件 。 所以,一旦你设置好了,你可以在你的控制器中调用一个函数,这个函数会自动将你所有的行从表中导出到excel。 但是,由于只需要导出一些行,因此可以将行的ID发送回控制器function,该function可以根据所选ID创build标准查询,并自动将其导出到Excel。