排班自定义excel导出

mvn

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
    </dependency>

impl

  @Override
    public XSSFWorkbook exportRorateInfo(HttpServletResponse response, RotateInfoExport exportRorateInfo) {
        Assert.isTrue(ObjectUtils.isNotEmpty(exportRorateInfo.getYearId()),"年级id不能为空");
Integer timecount=0;
        List<RotateInfoExport> mapList= rotateMapper.exportRorateInfo(exportRorateInfo.getYearId());
        if(mapList.size() == 0){
            throw new IllegalArgumentException("年级id有误");
        }

        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = new LinkedHashMap<>();
        map = new LinkedHashMap<>();
        map.put("学生姓名", "学生\\时间");
        //添加时间,取最长时间段
        for(int i=0;i<mapList.size();i++) {
            if (timecount < mapList.get(i).getData().size()) {
                timecount = mapList.get(i).getData().size();
                for (int j = 0; j < mapList.get(i).getData().size(); j++) {
                    map.put("时间" + j, mapList.get(i).getData().get(j).get("time"));
                }
            }
        }
        list.add(map);
        for(int i=0;i<mapList.size();i++){
            //添加科室列表
            map = new LinkedHashMap<>();
            map.put("学生姓名",mapList.get(i).getStudentName());
            //for(int j=0;j<mapList.get(i).getData().size();j++){
            for(int j=0;j<timecount;j++){
                if(j<mapList.get(i).getData().size()) {
                    map.put("时间" + j, mapList.get(i).getData().get(j).get("department") != null ? mapList.get(i).getData().get(j).get("department") : "");
                }
                else {
                    map.put("时间" + j,"");
                }
            }
            list.add(map);
        }
        if(list.size() == 0){
            throw new IllegalArgumentException("导出失败");
        }


//            json 转 excel
        try {
            XSSFWorkbook xssfWorkbook = ExcelUtils.map_to_excel(response, list, exportRorateInfo.getYearName(), exportRorateInfo.getYearName() + "轮转排班");
            return xssfWorkbook;
        } catch (IOException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("导出失败");
        }
    }

controller

 @PostMapping("/export")
    public void export(HttpServletResponse response,RotateInfoExport rotateInfoExport)
    {
//增加前置判断有没有数据
        HashMap<String,Object> object = SpringUtils.getBean(RedisCache.class).getCacheObject("arrange"+rotateInfoExport.getYearId());
        Assert.isTrue(ObjectUtils.isNotEmpty(object),"没有数据不能导出");
        XSSFWorkbook xssfWorkbook = rotateService.exportRorateInfo(response, rotateInfoExport);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        try {
            OutputStream os = response.getOutputStream();
            xssfWorkbook.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("导出失败!");
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值