esaypoi 导出下载 解决部分内容有问题,需要恢复

  <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
            <scope>compile</scope>
        </dependency>
  List<WbsTreeVo> wbsTreeVoInfos = projectService.getWbsTreeVoInfos(projectId);
        List<WbsTreeExcelModel> excelModels = JsonUtil.getJsonToList(wbsTreeVoInfos, WbsTreeExcelModel.class);
        ExportParams exportParams = new ExportParams();
        exportParams.setType(ExcelType.XSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, WbsTreeExcelModel.class, excelModels);
ExcelExportUtils.downLoadExcel(projectId+".xlsx",workbook);

public static void downLoadExcel(String fileName, Workbook workbook) {
        OutputStream out = null;
        BufferedOutputStream buf = null;
        try {
            HttpServletResponse response= ServletUtils.getResponse();
            response.reset(); //重置响应对象
            out = response.getOutputStream();
            buf = new BufferedOutputStream(out);

            String excelName = new String(fileName.getBytes("utf-8"), "iso-8859-1");
            response.setHeader("content-Type", "application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + excelName);
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);

            workbook.write(buf);
            buf.flush();
            out.close();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            workbook.write(baos);
            response.setHeader("Content-Length", String.valueOf(baos.size()));
            out.write( baos.toByteArray() );
        } catch (IOException e) {
            log.error("文件导出异常,文件名:{},异常信息:", fileName, e);
            ExceptionCast.cast(-1,"导出失败:"+e.getMessage());

        } finally {
            try {
                if (null != buf) {
                    buf.close();
                    buf = null;
                }
                if (null != out) {
                    out.close();
                    out = null;
                }
                System.gc();
            } catch (Exception e) {
                log.error("文件导出,关闭流异常:", e);
            }
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用EasyPoi导出表格文档,可以按照以下步骤进行操作: 1. 首先,可以在Maven中添加EasyPoi的依赖,例如: ``` <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.1.1</version> </dependency> ``` 2. 创建实体类,用于存储表格中的数据。 3. 在Controller中编写导出的方法,例如: ``` @RequestMapping("/export") public void export(HttpServletResponse response) { List<User> userList = userService.getAllUsers(); // 设置导出的文件名 String fileName = "用户信息表格.xls"; try { // 设置响应头信息 response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1")); // 调用EasyPoi的API,完成导出 ExcelExportUtil.exportExcel(new ExportParams("用户信息表", "用户信息"), User.class, userList, response.getOutputStream(), ExcelType.XLS); } catch (IOException e) { e.printStackTrace(); } } ``` 在上面的代码中,ExcelExportUtil.exportExcel方法用于导出Excel文件,其中ExportParams用于设置导出参数,User.class表示要导出的实体类,userList是要导出的数据列表,response.getOutputStream()用于获取输出流,ExcelType.XLS表示导出的文件类型为XLS格式。 4. 最后,访导出的方法即可完成表格的导出需要注意的是,以上仅是EasyPoi导出表格的基本操作,具体的使用还需要根据实际情况进行调整和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值