@RequestMapping("/exportExcel")
public void export(Model model, DataTable dataTable, HttpServletRequest request, HttpServletResponse response) {
PageRequest pageRequest = dataTable.toPageRequest();
pageRequest.setPageNo(1);
pageRequest.setPageSize(10);
Page page = Entity.create(entityClass).page(pageRequest, filter(model, request));
int totalPage = page.getTotalPages();
Iterator it = page.iterator();
Excel excel = ExcelFactory.createExcel("export.xls");
excel.setWorkingSheet(0);
CellStyle cellStyle = excel.getWorkBook().createCellStyle();
cellStyle.setWrapText(true);
AgencyWithdrawEntity item = null;
String[] title = { "代理名称", "开户行", "抬头/户名", "卡号","提现金额", "货币代码", "货币名称", "人民币转账", "参考汇率", "状态", "申请时间" ,"失败原因"};
for (int i = 0; i < title.length; i++) {
excel.cell(0, i).value(title[i]);
}
CellEditor editor = null;
int index = 1;
for (int i = 1; i <= totalPage; i++) {
while (it.hasNext()) {
item = it.next();
editor = excel.cell(index, 0);
editor.value(StringUtils.isNotBlank(item.getAgencyName()) ? item.getAgencyName() : "" );
editor = excel.cell(index, 1);
editor.value((StringUtils.isNotBlank(item.getBankName()) ? item.getBankName() : ""));
editor = excel.cell(index, 2);
editor.value(item.getAccountName());
editor = excel.cell(index, 3);
editor.value(item.getBankAccount());
editor = excel.cell(index, 4);
editor.value(new Money(item.getWithAmountSrt()));
editor = excel.cell(index, 5);
editor.value(item.getMoneyName());
editor = excel.cell(index, 6);
editor.value(item.getMoneyTitle());
editor = excel.cell(index, 7);
editor.value(item.getRmbAmountSrt());
editor = excel.cell(index, 8);
double rate;
if (item.getCurrentRate() ==null ) {
rate=0;
}else
rate=item.getCurrentRate()/1.035;
DecimalFormat format = new DecimalFormat("#####0.000000");
editor.value(format.format(rate));
editor = excel.cell(index, 9);
editor.value(item.getStatusStr());
editor = excel.cell(index, 10);
editor.value(DateUtils.formatDate(item.getGmtCreate(), "yyyy-MM-dd HH:mm:ss"));
editor = excel.cell(index, 11);
editor.value(item.getBackReason());
index++;
}
page = Entity.create(entityClass).page(new PageRequest(i + 1, 10), filter(model, request));
it = page.iterator();
}
try {
response.addHeader("Content-Disposition", "attachment; filename=\"export.xls\"");
excel.saveExcel(response.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
分页查询 单次导出就是单页的数据啊,你要是想一次导出所有数据就写一个查询所有的方法。