5 POI報表導出
5.1 需求分析
完成當月人事報表的導出:包含當月入職員工信息,離職員工信息
java
5.2 人事報表導出
5.2.1 步驟分析
- 構造Excel表格數據
- 建立工做簿
- 建立sheet
- 建立行對象
- 建立單元格對象
- 填充數據,設置樣式
- 下載
5.2.2 代碼實現
(1)配置controllerweb
@RequestMapping(value = "/export/{month}", method = RequestMethod.GET) public void export(@PathVariable(name = "month") String month) throws Exception { //1.構造數據 List<EmployeeReportResult> list = userCompanyPersonalService.findByReport(companyId,month+"%"); //2.建立工做簿 XSSFWorkbook workbook = new XSSFWorkbook(); //3.構造sheet String[] titles = {"編號", "姓名", "手機","最高學歷", "國家地區", "護照號", "籍貫", "生日", "屬相","入職時間","離職類型","離職緣由","離職時間"}; Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); AtomicInteger headersAi = new AtomicInteger(); for (String title : titles) { Cell cell = row.createCell(headersAi.getAndIncrement()); cell.setCellValue(title); } AtomicInteger datasAi = new AtomicInteger(1); Cell cell = null; for (EmployeeReportResult report : list) { Row dataRow = sheet.createRow(datasAi.getAndIncrement()); //編號 cell = dataRow.createCell(0); cell.setCellValue(report.getUserId()); //姓名 cell = dataRow.createCell(1); cell.setCellValue(report.getUsername()); //手機 cell = dataRow.createCell(2); cell.setCellValue(report.getMobile()); //最高學歷 cell = dataRow.createCell(3); cell.setCellValue(report.getTheHighestDegreeOfEducation()); //國家地區 cell = dataRow.createCell(4); cell.setCellValue(report.getNationalArea()); //護照號 cell = dataRow.createCell(5); cell.setCellValue(report.getPassportNo()); //籍貫 cell = dataRow.createCell(6); cell.setCellValue(report.getNativePlace()); //生日 cell = dataRow.createCell(7); cell.setCellValue(report.getBirthday()); //屬相 cell = dataRow.createCell(8); cell.setCellValue(report.getZodiac()); //入職時間 cell = dataRow.createCell(9); cell.setCellValue(report.getTimeOfEntry()); //離職類型 cell = dataRow.createCell(10); cell.setCellValue(report.getTypeOfTurnover()); //離職緣由 cell = dataRow.createCell(11); cell.setCellValue(report.getReasonsForLeaving()); //離職時間 cell = dataRow.createCell(12); cell.setCellValue(report.getResignationTime()); } String fileName = URLEncoder.encode(month+"人員信息.xlsx", "UTF-8"); response.setContentType("application/octet-stream"); response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("ISO8859-1"))); response.setHeader("filename", fileName); workbook.write(response.getOutputStream()); }
(2)添加serviceapp
//根據企業id和年月查詢 public List<EmployeeReportResult> findByReport(String companyId, String month) { return userCompanyPersonalDao.findByReport(companyId,month); }
(3)dao層實現dom
@Query(value = "select new com.ihrm.domain.employee.response.EmployeeReportResult(a,b) " + "FROM UserCompanyPersonal a LEFT JOIN EmployeeResignation b ON a.userId=b.userId WHERE a.companyId = ?1 AND a.timeOfEntry LIKE ?2 OR (b.resignationTime LIKE ?2)") List<EmployeeReportResult> findByReport(String companyId, String month);
本文同步分享在 博客「cwl_java」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。svg