美文网首页
excel导出

excel导出

作者: Aluha_f289 | 来源:发表于2019-07-30 20:34 被阅读0次
        /**
         * @GET:pom-attendance-statics/doExport
         * 导出考勤报表统计
         * @param map
         *          orgId: 部门Id
         *          yearAndMonth:需要查询的日期
         *          userId: 用户Id
         * @return
         */
        @Override
        public ResponseEntity<Void> queryPomAttendanceStaticsByOrgAnUserDoExport(Map<String, String> map, HttpServletRequest request,
                HttpServletResponse response) {
            List<PomStatisticsVM> PomStatisticsVMList  = queryPomAttendanceStaticsByOrgAnUser(map);
            List<Map<String, Object>> listMap = new ArrayList<>();
            
            try {
                for (PomStatisticsVM pomStatisticsVM : PomStatisticsVMList) {
                    //将对象转化为map
                    Map<String, Object> mapTran = objectToMap(pomStatisticsVM);
                    listMap.add(mapTran);
                }
            
                if (null != listMap && listMap.size() > 0) {
                    String rUrl = request.getRequestURL().toString();
                    String rPort = rUrl.split("/")[2].split(":")[1];
                    String lPort = String.valueOf(request.getLocalPort());
                    // 乱码处理
                    if (!rPort.equals(lPort)) {
                        String url = rUrl.replace(rPort, lPort);
                        url += "?incomeIds=" + listMap.get(0).get("id");
                        response.sendRedirect(url);
                    } else {
                        HSSFWorkbook wb = new HSSFWorkbook();
                        HSSFSheet sheet = wb.createSheet("考勤统计报表");
                        HSSFCellStyle style = wb.createCellStyle();
                        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                        
                        // 创建一个居中格式
                        HSSFFont fontTitle = wb.createFont();
                        // 设置宋体
                        fontTitle.setFontName("宋体");
                        fontTitle.setFontHeightInPoints((short) 16);
                        fontTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                        HSSFCellStyle styleHead = wb.createCellStyle();
                        styleHead.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                        styleHead.setFont(fontTitle);
                        
                        // 表头区
                        HSSFRow rowTitle = sheet.createRow(0);
                        HSSFCell cellTitle = rowTitle.createCell(0);
                        cellTitle.setCellValue("考勤统计报表");
                        cellTitle.setCellStyle(styleHead);
                        sheet.addMergedRegion(new CellRangeAddress((short) 0, (short) 0, (short) 0, (short) 16));
                        // 标题区样式
                        HSSFCellStyle styleTitle = wb.createCellStyle();
                        HSSFFont fontContent = wb.createFont();
                        fontContent.setFontName("宋体");
                        fontContent.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                        styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                        // 创建一个居中格式
                        styleTitle.setFont(fontContent);
                        HSSFRow row = sheet.createRow(1);
                        
                        // 设置列的宽度
                        sheet.setColumnWidth(1, (short) 4000);
                        sheet.setColumnWidth(2, (short) 4000);
                        sheet.setColumnWidth(3, (short) 4000);
                        sheet.setColumnWidth(5, (short) 4000);
                        sheet.setColumnWidth(6, (short) 4000);
                        sheet.setColumnWidth(7, (short) 3000);
                        sheet.setColumnWidth(8, (short) 4000);
                        sheet.setColumnWidth(9, (short) 3000);
                        sheet.setColumnWidth(10, (short) 4000);
                        String[] name = new String[] { "班组", "工号","姓名", "年月", "出勤", "旷工", "病假",
                                        "事假", "产假", "婚假", "丧假", "探亲","年休", "加班", "迟到", "早退", "补卡" };
                        
                        for (int _i = 0; _i < listMap.size(); _i++) {
                            // 消耗
                            Map<String, Object> map2 = listMap.get(_i);
                            for (int i = 0; i < name.length; i++) {
                              HSSFCell cel1 = row.createCell(i);
                              cel1.setCellValue(name[i]);
                              cel1.setCellStyle(styleTitle);
                            }
                            
                            HSSFRow row1 = sheet.createRow(_i + 2);
                            style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
                            HSSFFont fontBody = wb.createFont();
                            // 设置宋体
                            fontBody.setFontName("宋体");
                            style.setFont(fontBody);
                            
                            //遍历map赋值
                            for (int j = 0; j < name.length; j++) {
                                HSSFCell cel2 = row1.createCell(j);
                                if (name[j].equals("班组")) {
                                    if (map2.get("orgName") == null) {
                                      cel2.setCellValue("");
                                    } else {
                                      cel2.setCellValue(map2.get("orgName").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("工号")) {
                                    if (map2.get("code") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("code").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("姓名")) {
                                    if (map2.get("userName") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("userName").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("年月")) {
                                    if (map2.get("yearMonth") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("yearMonth").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("出勤")) {
                                    if (map2.get("workDay") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("workDay").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("旷工")) {
                                    if (map2.get("absenteeism") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("absenteeism").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("病假")) {
                                    if (map2.get("sickLeave") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("sickLeave").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("事假")) {
                                    if (map2.get("compassionateLeave") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("compassionateLeave").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("产假")) {
                                    if (map2.get("maternityLeave") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("maternityLeave").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("婚假")) {
                                    if (map2.get("wfLeave") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("wfLeave").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("丧假")) {
                                    if (map2.get("funeral") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("funeral").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("探亲")) {
                                    if (map2.get("visit") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("visit").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("年休")) {
                                    if (map2.get("hugh") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("hugh").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("加班")) {
                                    if (map2.get("overTime") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("overTime").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("迟到")) {
                                    if (map2.get("lateDegree") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("lateDegree").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("早退")) {
                                    if (map2.get("leaveDegree") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("leaveDegree").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }else if (name[j].equals("补卡")) {
                                    if (map2.get("supplementNum") == null) {
                                        cel2.setCellValue("");
                                    } else {
                                        cel2.setCellValue(map2.get("supplementNum").toString());
                                    }
                                    cel2.setCellStyle(style);
                                }          
                                
                            }
                        }
                        String orgId = map.get("orgId") == null?"0": map.get("orgId");
                        String yearAndMonth = map.get("yearAndMonth");
                        String year = yearAndMonth.substring(0, 4);
                        String month = yearAndMonth.substring(yearAndMonth.length() - 1, yearAndMonth.length());
                        SysOrg sysOrg = sysOrgRepository.findOne(Long.parseLong(orgId));
                        
                        response.setCharacterEncoding("UTF-8");
                        String fileName = null;
                        if (sysOrg != null) {
                            fileName = URLEncoder.encode(year + "年_" + month + "月"  + "(" +sysOrg.getOrgName() + ")" + "-考勤统计报表" + ".xls", "UTF-8");
                        }else {
                            fileName = URLEncoder.encode(year + "年_" + month + "月" + "-考勤统计报表" + ".xls", "UTF-8");
                        }
                         
                        response.setContentType("application/octet-stream");
                        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                        OutputStream ouputStream = response.getOutputStream();
                        wb.write(ouputStream);
                        ouputStream.flush();
                        ouputStream.close();
                    }
                }
            
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return null;
        }
    

    相关文章

      网友评论

          本文标题:excel导出

          本文链接:https://www.haomeiwen.com/subject/vzyorctx.html