导出功能接口

本文详细介绍了如何在Java中实现导出功能接口,包括数据处理、文件格式化及导出过程中的常见问题和解决方案,帮助开发者提升数据导出效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
    @GetMapping(value = "/exportData")
    public void exportData(Integer policyId, HttpServletResponse response) {
        String platFromId = SecurityUtils.getSubject().getSession().getAttribute("platFromId").toString();
        PolicyForm policyForm = policyFormMapper.selectByPolicyId(policyId);
        try {
            //1、创建workbook,对应一个excel
            HSSFWorkbook wb = new HSSFWorkbook();

            //1.5、生成excel中可能用到的单元格样式
            //首先创建字体样式
            HSSFFont font = wb.createFont();//创建字体样式
            font.setFontName("宋体");//使用宋体
            font.setFontHeightInPoints((short) 10);//字体大小
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
            HSSFSheet sheet = wb.createSheet();
            HSSFRow hssfRow = sheet.createRow(0);
            hssfRow.createCell(0).setCellValue("序号");
            hssfRow.createCell(1).setCellValue("企业名称");
            hssfRow.createCell(2).setCellValue("社会统一信用代码");
            hssfRow.createCell(3).setCellValue("申报状态");
            hssfRow.createCell(4).setCellValue("办公楼宇");
            hssfRow.createCell(5).setCellValue("申报时间");
            hssfRow.createCell(6).setCellValue("申报备注");
            String entBasic = policyForm.getEntBasic();
            String indicator = policyForm.getIndicator();
            int cellColumn = 7;
            if (StringUtil.isNotEmpty(entBasic)) {
                JSONArray entBasicJson = JSON.parseArray(entBasic);
                for (int i = 0; i<entBasicJson.size(); i++) {
                    hssfRow.createCell(cellColumn).setCellValue(entBasicJson.getJSONObject(i).get("title").toString());
                    cellColumn++;
                }
            }
           if (StringUtil.isNotEmpty(indicator)) {
               JSONArray indicatorJson = JSON.parseArray(indicator);
               for (int i = 0; i<indicatorJson.size(); i++) {
                   hssfRow.createCell(cellColumn).setCellValue(indicatorJson.getJSONObject(i).get("title").toString());
                   cellColumn++;
               }
           }

            List<PolicyDeclare> policyDeclares = policyDeclareMapper.selectByPolicyId(policyId);
            for (int j = 1; j <=policyDeclares.size(); j++) {
                hssfRow = sheet.createRow(j);
                hssfRow.createCell(0).setCellValue(j);
                hssfRow.createCell(1).setCellValue(policyDeclares.get(j-1).getEntName());
                hssfRow.createCell(2).setCellValue(policyDeclares.get(j-1).getCreditCode());
                if (policyDeclares.get(j-1).getStatus() == 1) {
                    hssfRow.createCell(3).setCellValue("待审核");
                } else if (policyDeclares.get(j-1).getStatus() == 2) {
                    hssfRow.createCell(3).setCellValue("审核通过");
                } else if (policyDeclares.get(j-1).getStatus() == 2) {
                    hssfRow.createCell(3).setCellValue("审核驳回");
                }
                String eid = policyDeclares.get(j - 1).getLinkValue();
                if (StringUtil.isNotEmpty(eid)) {
                    EntFile entFile = entFileDao.findByEid(eid, platFromId);
                    if (StringUtil.isNotEmpty(entFile.getOfficeBuildingIds())) {
                        String[] layoutIds = entFile.getOfficeBuildingIds().split(",");
                        StringBuilder layoutName = new StringBuilder();
                        for (String layoutId: layoutIds) {
                            Layout layout = layoutService.findById(Integer.valueOf(layoutId));
                            layoutName.append(layout.getName() == null ? null: layout.getName() + ",");
                        }
                        layoutName.deleteCharAt(layoutName.lastIndexOf(","));
                        hssfRow.createCell(4).setCellValue(layoutName.toString());
                    }
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                hssfRow.createCell(5).setCellValue(policyDeclares.get(j-1).getCreateTime() == null ? null: sdf.format(policyDeclares.get(j-1).getCreateTime()));
                hssfRow.createCell(6).setCellValue(policyDeclares.get(j-1).getRemark());

                String entBasicData = policyDeclares.get(j - 1).getEntBasicData();
                String indicatorData = policyDeclares.get(j - 1).getIndicatorData();
                cellColumn = 7;
                if (StringUtil.isNotEmpty(entBasicData)) {
                    JSONArray entBasicDataJson = JSON.parseArray(entBasicData);
                    for (int i = 0; i<entBasicDataJson.size(); i++) {
                        if (entBasicDataJson.getJSONObject(i).containsKey("value")) {
                            hssfRow.createCell(cellColumn).setCellValue(entBasicDataJson.getJSONObject(i).get("value").toString());
                        }
                        cellColumn++;
                    }
                }
                if (StringUtil.isNotEmpty(indicatorData)) {
                    JSONArray indicatorDataJson = JSON.parseArray(indicatorData);
                    for (int i = 0; i<indicatorDataJson.size(); i++) {
                        if (indicatorDataJson.getJSONObject(i).containsKey("value")) {
                            hssfRow.createCell(cellColumn).setCellValue(indicatorDataJson.getJSONObject(i).get("value").toString());
                        }
                        cellColumn++;
                    }
                }
            }
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            String fileName = new String("企业填报数据.xls".getBytes("gb2312"), "ISO8859-1");

            //response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; fileName=" + fileName);
            wb.write(response.getOutputStream());
        } catch (Exception e) {
            logger.error("企业填报数据导出错误", e);
        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值