【发布时间】:2014-07-15 09:55:58
【问题描述】:
我在表格中有数据。每行都有一个复选框。当用户单击导出按钮时,我希望将检查的行导出到 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。
将选定的行单独下载到 Excel。
【问题讨论】:
-
那么实际的问题是什么?你试过什么?
-
使用上面的代码,我可以导出整个表格数据。但不是我在复选框中选择的特定行。
-
你能帮我@cfrick吗?
标签: jquery excel grails html-table