標籤:https xlsx attribute 資料許可權 ges catch 技術 分享 filename
下面是jsp代碼:
<li class="btns"><input id="btnExport" class="btn btn-primary" type="button" value="匯出所有" onclick="exports()" /></li>
function exports(){ top.$.jBox.confirm("確認要匯出所有資料嗎?","系統提示",function(v,h,f){ if(v=="ok"){ $("#searchForm").attr("action","${ctx}/record/record/record/export"); $("#searchForm").submit(); } },{buttonsFocus:1}); top.$(‘.jbox-body .jbox-icon‘).css(‘top‘,‘55px‘); }
下面是Controller代碼: 我這個是匯出的一對多 並且傳入的是實體類:
@RequestMapping(value = "export") public String exportFile(Record record, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) { try { String fileName = "使用者資料"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx"; Page<Record> page = recordService.findRecord(new Page<Record>(request, response, -1), record); new ExportExcel("使用者資料", Record.class).setDataList(page.getList()).write(response, fileName).dispose(); return null; } catch (Exception e) { addMessage(redirectAttributes, "匯出失敗!失敗資訊:"+e.getMessage()); } return "redirect:" + adminPath + "/record/record/record?repage"; }
service層:
public Page<Record> findRecord(Page<Record> page, Record record) { // 產生資料許可權過濾條件(dsf為dataScopeFilter的簡寫,在xml中使用 ${sqlMap.dsf}調用許可權SQL) record.getSqlMap().put("dsf", dataScopeFilter(record.getCurrentUser(), "o", "a")); // 設定分頁參數 record.setPage(page); // 執行分頁查詢 page.setList(recordDao.findList(record)); return page; }
想要匯出的實體類欄位的get方法上邊要寫上註解:
@ExcelField(title="模式", align=2, sort=800, fieldType=RoleListType.class)
想要通過主表匯出子表的資料
一定要在主表裡面把子表的每一個欄位都寫出來,並且產生get set方法
在get方法上邊寫上上邊的註解。
java excel匯出