SAAS-08-员工管理及POI

93 阅读3分钟

第7章 POI报表的入门

理解员工管理的的业务逻辑 能够说出Eureka和Feign的作用 理解报表的两种形式和POI的基本操作 熟练使用POI完成Excel的导入导出操作 1 员工管理 1.1 需求分析 企业员工管理是人事资源管理系统中最重要的一个环节,分为对员工入职,转正,离职,调岗,员工报表导入导出 等业务逻辑。需求看似复杂,实际上都是对数据库表的基本操作。

1.3 代码实现 由于此部分内容全部围绕的基本CRUD操作,为了节省课程时间,员工管理的代码以资料的形式给各位学员下发, 学员们直接导入到工程即可。重点功能突出讲解即可 1.3.1 服务端实现 (1) 创建员工微服务 ihrm_employee (2) 配置文件 application.yml (3) 配置Shiro核心配置类ShiroConfiguration (4) 配置启动类EmployeeApplication (5)导入资源中提供的基本Controller,Service,Dao,Domain代码

前端代码和后端代码直接导入就行,就是复制工程得代码就行,主要事理解就行,其他得都不难

Eureka注册

1.5 微服务调用组件Feign

1.5.1 简介 Feign是简化Java HTTP客户端开发的工具(java-to-httpclient-binder),它的灵感来自于Retrofit、JAXRS-2.0和 WebSocket。

Feign的初衷是降低统一绑定Denominator到HTTP API的复杂度,不区分是否为restful 1.5.2 快速体验 我们现在在系统微服务调用企业微服务的方法(根据ID查询部门)

进行Feign测试

远程接口调用成功

2 POI报表的概述

2.1 需求说明 在企业级应用开发中,Excel报表是一种最常见的报表需求。Excel报表开发一般分为两种形式: 为了方便操作,基于Excel的报表批量上传数据 通过java代码生成Excel报表。 在Saas-HRM系统中,也有大量的报表操作,那么接下来的课程就是一起来学习企业级的报表开发。

2.4 POI的概述 Apache POI是Apache软件基金会的开源项目,由Java编写的免费开源的跨平台的 Java API,Apache POI提供API 给Java语言操作Microsoft Office的功能。 2.5 POI的应用场景 1. 数据报表生成 2. 数据备份 3. 数据批量上传

批量导入用户数据:


    /**
     * 导入Excel,添加用户
     *  文件上传:springboot
     */
    @RequestMapping(value="/user/import",method = RequestMethod.POST)
    public Result importUser(@RequestParam(name="file") MultipartFile file) throws Exception {
        //1.解析Excel
        //1.1.根据Excel文件创建工作簿
        Workbook wb = new XSSFWorkbook(file.getInputStream());
        //1.2.获取Sheet
        Sheet sheet = wb.getSheetAt(0);//参数:索引
        //1.3.获取Sheet中的每一行,和每一个单元格
        //2.获取用户数据列表
        List<User> list = new ArrayList<>();
        System.out.println(sheet.getLastRowNum());
        for (int rowNum = 1; rowNum<= sheet.getLastRowNum() ;rowNum ++) {
            Row row = sheet.getRow(rowNum);//根据索引获取每一个行
            Object [] values = new Object[row.getLastCellNum()];
            for(int cellNum=1;cellNum< row.getLastCellNum(); cellNum ++) {
                Cell cell = row.getCell(cellNum);
                Object value = getCellValue(cell);
                values[cellNum] = value;
            }
            User user = new User(values);
            list.add(user);
        }
        //3.批量保存用户
        userService.saveAll(list,companyId,companyName);

        return new Result(ResultCode.SUCCESS);
    }

进行导入测试:

进行导入测试:

导入成功

数据库中保存数据

5 POI报表导出

5.1 需求分析 完成当月人事报表的导出:包含当月入职员工信息,离职员工信息

5.2 人事报表导出

5.2.1 步骤分析 构造Excel表格数据 创建工作簿 创建sheet 创建行对象 创建单元格对象 填充数据,设置样式 下载 5.2.2 代码实现

当月人事报表得导出工作

 @RequestMapping(value = "/export/{month}", method = RequestMethod.GET)
    public void export(@PathVariable String month) throws Exception {
        //1.获取报表数据
        List<EmployeeReportResult> list = userCompanyPersonalService.findByReport(companyId,month);

        //2.加载模板
        Resource resource = new ClassPathResource("excel-template/hr-demo.xlsx");
        FileInputStream fis = new FileInputStream(resource.getFile());

        //3.根据模板创建工作簿
        Workbook wb = new XSSFWorkbook(fis);
        //4.读取工作表
        Sheet sheet = wb.getSheetAt(0);
        //5.抽取公共样式
        Row row = sheet.getRow(2);
        CellStyle styles [] = new CellStyle[row.getLastCellNum()];
        for(int i=0;i<row.getLastCellNum();i++) {
            Cell cell = row.getCell(i);
            styles[i] = cell.getCellStyle();
        }
        //6.构造单元格
        int rowIndex = 2;
        Cell cell=null;
        for (EmployeeReportResult employeeReportResult : list) {
            row = sheet.createRow(rowIndex++);
//            for(int j=0;j<styles.length;j++) {
//                cell = row.createCell(j);
//                cell.setCellStyle(styles[j]);
//            }

            // 编号,
            cell = row.createCell(0);
            cell.setCellValue(employeeReportResult.getUserId());
            cell.setCellStyle(styles[0]);
            // 姓名,
            cell = row.createCell(1);
            cell.setCellValue(employeeReportResult.getUsername());
            cell.setCellStyle(styles[1]);
            // 手机,
            cell = row.createCell(2);
            cell.setCellValue(employeeReportResult.getMobile());
            cell.setCellStyle(styles[2]);
            // 最高学历,
            cell = row.createCell(3);
            cell.setCellValue(employeeReportResult.getTheHighestDegreeOfEducation());
            cell.setCellStyle(styles[3]);
            // 国家地区,
            cell = row.createCell(4);
            cell.setCellValue(employeeReportResult.getNationalArea());
            cell.setCellStyle(styles[4]);
            // 护照号,
            cell = row.createCell(5);
            cell.setCellValue(employeeReportResult.getPassportNo());
            cell.setCellStyle(styles[5]);
            // 籍贯,
            cell = row.createCell(6);
            cell.setCellValue(employeeReportResult.getNativePlace());
            cell.setCellStyle(styles[6]);
            // 生日,
            cell = row.createCell(7);
            cell.setCellValue(employeeReportResult.getBirthday());
            cell.setCellStyle(styles[7]);
            // 属相,
            cell = row.createCell(8);
            cell.setCellValue(employeeReportResult.getZodiac());
            cell.setCellStyle(styles[8]);
            // 入职时间,
            cell = row.createCell(9);
            cell.setCellValue(employeeReportResult.getTimeOfEntry());
            cell.setCellStyle(styles[9]);
            // 离职类型,
            cell = row.createCell(10);
            cell.setCellValue(employeeReportResult.getTypeOfTurnover());
            cell.setCellStyle(styles[10]);
            // 离职原因,
            cell = row.createCell(11);
            cell.setCellValue(employeeReportResult.getReasonsForLeaving());
            cell.setCellStyle(styles[11]);
            // 离职时间
            cell = row.createCell(12);
            cell.setCellValue(employeeReportResult.getResignationTime());
            cell.setCellStyle(styles[12]);
        }
        //7.下载
        //3.完成下载
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        new DownloadUtils().download(os,response,month+"人事报表.xlsx");

导出报表成功:

百万数据测试: