java导出Excel表

该文章展示了如何在Java应用中使用Easypoi库来处理Excel导出。通过添加Easypoi的依赖,代码示例说明了如何基于API创建Excel工作簿,设置标题和内容,以及如何通过HttpServletResponse进行文件响应,将生成的Excel文件以附件形式下载。

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

pom引入依赖:

        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.1.0</version>
        </dependency>

代码示例:

@ApiOperation("导出分类内容")
    @PostMapping(value = UrlRuleConstants.CLASS_EXPORT, produces = UrlRuleConstants.PRODUCES)
    public void exportMoreSheet(HttpServletResponse response, @RequestBody IdsHandoverLogVo vo) {
        UserInfo userInfo = CommonUtil.filterCpmoCopForString();
      
        List<HandoverLogExcelVo> list = new ArrayList<>();
        List<HandoverExcelVo> exportContentList = getExportContentList(vo, userInfo);
        if (!HandoverTypeEnum.SUPPLY_GAS_EXPLAIN.getCode().equals(vo.getType())) {
            for (HandoverExcelVo excelVo : exportContentList) {
                HandoverLogExcelVo handoverLogExcelVo = new HandoverLogExcelVo();
                handoverLogExcelVo.setClassType(excelVo.getClassType());
                handoverLogExcelVo.setHandoverPerson(excelVo.getHandoverPerson());
                handoverLogExcelVo.setHandoverTime(excelVo.getHandoverTime());
                handoverLogExcelVo.setValue(excelVo.getValue());
                list.add(handoverLogExcelVo);
            }
        }

        //1.分别构建客户与场站信息导出参数
        ExportParams exportParam = new ExportParams();
        //标题
        exportParam.setTitle(HandoverTypeEnum.getDescBycode(vo.getType()));


        //sheet标题
        exportParam.setSheetName("分类内容");
        exportParam.setHeight((short) 8);
        exportParam.setStyle(ExcelStyleUtil.class);

        //2.构建分别填充数据:data(数据)、title(标题)、entity(导出属性)
        Map<String, Object> exportCustomerMap = new HashMap<String, Object>();
        if (vo.getType() == 1) {
            exportCustomerMap.put("data", exportContentList);
            exportCustomerMap.put("entity", HandoverExcelVo.class);
        } else {
            exportCustomerMap.put("data", list);
            exportCustomerMap.put("entity", HandoverLogExcelVo.class);
        }
        exportCustomerMap.put("title", exportParam);

        //3.组装参数
        List<Map<String, Object>> sheetsList = new ArrayList<Map<String, Object>>();
        sheetsList.add(exportCustomerMap);
        try {
            //4.调用导出接口
            Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
            String fileName = "分类内容";
            fileName = URLEncoder.encode(fileName, "UTF8");
            response.setContentType("application/vnd.ms-excel;chartset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值