数据库取值返回excel

 File tempFile = null;
        try {
            //新建mybatisplus查询对象
            QueryWrapper<BizIntention> queryWrapper = new QueryWrapper<>();
            if(ObjectUtil.isNotEmpty(bizIntentionExportParam)){
                queryWrapper.lambda().in(BizIntention::getId,StrUtil.split(bizIntentionExportParam.getIntentionIds(),StrUtil.COMMA));
            }

            List<BizIntention> bizIntentions = this.list(queryWrapper);

            //获取字典值
            QueryWrapper<BizDict> dictParamQueryWrapper = new QueryWrapper<>();
            //只查询"DICT_LABEL,DICT_VALUE"两列
            dictParamQueryWrapper.select("DICT_LABEL,DICT_VALUE");
            List<BizDict> objects = bizDictMapper.selectList(dictParamQueryWrapper);
            ArrayList<DictParam> dictParamList = new ArrayList<>();
            for (BizDict object : objects) {
                DictParam dictParam = new DictParam();
                BeanUtil.copyProperties(object,dictParam);
                dictParamList.add(dictParam);
            }
            Map<String, String> hashMap = dictParamList.stream().collect(
                    Collectors.toMap(
                            dictParam -> dictParam.getDictValue(),
                            dictParam -> dictParam.getDictLabel()
                    )
            );

            //循环拷贝所有数据至result对象
            List<BizIntentionExportResult> bizIntentionExportResults = new ArrayList<>();
            for (int i = 0; i < bizIntentions.size(); i++) {
                BizIntentionExportResult bizIntentionExportResult = new BizIntentionExportResult();
                BeanUtil.copyProperties(bizIntentions.get(i),bizIntentionExportResult);

                //如果专业领域匹配到,就拿值
                if (hashMap.containsKey(bizIntentionExportResult.getProfessional())){
                    bizIntentionExportResult.setProfessional(hashMap.get(bizIntentionExportResult.getProfessional()));
                }
                //如果项目来源匹配到,就拿值
                if (hashMap.containsKey(bizIntentionExportResult.getProjectSource())){
                    bizIntentionExportResult.setProjectSource(hashMap.get(bizIntentionExportResult.getProjectSource()));
                }
                //如果项目类别匹配到,就拿值
                if (hashMap.containsKey(bizIntentionExportResult.getProjectCaregory())){
                    bizIntentionExportResult.setProjectCaregory(hashMap.get(bizIntentionExportResult.getProjectCaregory()));
                }
                //如果研究类型匹配到,就拿值
                if (hashMap.containsKey(bizIntentionExportResult.getStudyType())){
                    bizIntentionExportResult.setStudyType(hashMap.get(bizIntentionExportResult.getStudyType()));
                }
                //如果成果类别匹配到,就拿值
                if (hashMap.containsKey(bizIntentionExportResult.getResultCategory())){
                    bizIntentionExportResult.setResultCategory(hashMap.get(bizIntentionExportResult.getResultCategory()));
                }
                //如果企业导师职称匹配到,就拿值
                if (hashMap.containsKey(bizIntentionExportResult.getTitleSupervisor())){
                    bizIntentionExportResult.setTitleSupervisor(hashMap.get(bizIntentionExportResult.getTitleSupervisor()));
                }
                //如果企业导师最高学历匹配到,就拿值
                if (hashMap.containsKey(bizIntentionExportResult.getMentorEducation())){
                    bizIntentionExportResult.setMentorEducation(hashMap.get(bizIntentionExportResult.getMentorEducation()));
                }
                //如果是否为项目负责人匹配到,就拿值
                if (hashMap.containsKey(bizIntentionExportResult.getIsProjectLeader())){
                    bizIntentionExportResult.setIsProjectLeader(hashMap.get(bizIntentionExportResult.getIsProjectLeader()));
                }
                //如果学校名称匹配到,就拿值
                if (hashMap.containsKey(bizIntentionExportResult.getSchoolName())){
                    bizIntentionExportResult.setSchoolName(hashMap.get(bizIntentionExportResult.getSchoolName()));
                }
                //如果是否接受联培学校调剂
                //匹配到,就拿值
                if (hashMap.containsKey(bizIntentionExportResult.getIsjointTraining())){
                    bizIntentionExportResult.setIsjointTraining(hashMap.get(bizIntentionExportResult.getIsjointTraining()));
                }
                //如果是否接受学生类型调剂
                //匹配到,就拿值
                if (hashMap.containsKey(bizIntentionExportResult.getIsstudentType())){
                    bizIntentionExportResult.setIsstudentType(hashMap.get(bizIntentionExportResult.getIsstudentType()));
                }
                bizIntentionExportResult.setCreateTime(bizIntentionExportResult.getCreateTime());

                bizIntentionExportResults.add(bizIntentionExportResult);
            }


            if (ObjectUtil.isEmpty(bizIntentionExportResults)){
                throw new CommonException("无数据可导出");
            }
            String fileName = "需求意向表.xlsx";
            //bizIntentions = CollectionUtil.sort(bizIntentions, Comparator.comparing(BizIntention::getId));
            // 创建临时文件
            tempFile = FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + fileName);

            /*List<BizIntentionExportResult> bizIntentionExportResults = bizIntentions.stream()
                    .map(bizIntention -> {
                        BizIntentionExportResult bizIntentionExportResult = new BizIntentionExportResult();
                        BeanUtil.copyProperties(bizIntention, bizIntentionExportResult);
                        BizUserExportResult bizUserExportResult =new BizUserExportResult();
                        // 状态枚举转为文字
                        bizUserExportResult.setUserStatus(bizUserExportResult.getUserStatus()
                                .equalsIgnoreCase(BizUserStatusEnum.ENABLE.getValue())?"正常":"停用");
                        // 将base64转为byte数组
                        if (ObjectUtil.isNotEmpty(bizUser.getAvatar())) {
                            bizUserExportResult.setAvatar(ImgUtil.toBytes(ImgUtil.toImage(StrUtil
                                    .split(bizUser.getAvatar(), StrUtil.COMMA).get(1)), ImgUtil.IMAGE_TYPE_PNG));
                        }
                        return bizUserExportResult;
                    }).collect(Collectors.toList());*/

            // 头的策略
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short) 14);
            headWriteCellStyle.setWriteFont(headWriteFont);
            // 水平垂直居中
            headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            // 内容的策略
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
            contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            // 内容背景白色
            contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            WriteFont contentWriteFont = new WriteFont();

            // 内容字体大小
            contentWriteFont.setFontHeightInPoints((short) 12);
            contentWriteCellStyle.setWriteFont(contentWriteFont);

            //设置边框样式,细实线
            contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
            contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
            contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
            contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);


            // 水平垂直居中
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
            contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle,
                    contentWriteCellStyle);

            // 写excel
            EasyExcel.write(tempFile.getPath(), BizIntentionExportResult.class)
                    // 自定义样式
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    // 自动列宽
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())

                    // 机构分组合并单元格
                    /*.registerWriteHandler(new CommonExcelCustomMergeStrategy(bizIntentions.stream().map(BizIntentionExportResult::g)
                            .collect(Collectors.toList()), 0))*/
                    // 设置第一行字体

                    .registerWriteHandler(new CellWriteHandler() {
                        @Override
                        public void afterCellDispose(CellWriteHandlerContext context) {
                            WriteCellData<?> cellData = context.getFirstCellData();
                            WriteCellStyle writeCellStyle = cellData.getOrCreateStyle();
                            Integer rowIndex = context.getRowIndex();
                            if(rowIndex == 0) {
                                WriteFont headWriteFont = new WriteFont();
                                headWriteFont.setFontName("宋体");
                                headWriteFont.setBold(true);
                                headWriteFont.setFontHeightInPoints((short) 16);
                                writeCellStyle.setWriteFont(headWriteFont);
                            }
                        }
                    })
                    // 设置表头行高
                    .registerWriteHandler(new AbstractRowHeightStyleStrategy() {
                        @Override
                        protected void setHeadColumnHeight(Row row, int relativeRowIndex) {
                            if(relativeRowIndex == 0) {
                                // 表头第一行
                                row.setHeightInPoints(34);
                            } else {
                                // 表头其他行
                                row.setHeightInPoints(30);
                            }
                        }
                        @Override
                        protected void setContentColumnHeight(Row row, int relativeRowIndex) {
                            // 内容行
                            row.setHeightInPoints(20);
                        }
                    })
                    .sheet("需求意向")
                    .doWrite(bizIntentionExportResults);
            CommonDownloadUtil.download(tempFile, response);
        }catch (Exception e) {
            log.error(">>> 需求意向导出异常:", e);
            CommonResponseUtil.renderError(response, "导出失败");
        } finally {
            FileUtil.del(tempFile);
        }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值