EasyPoi实现网络图片的Excel导出功能

导入EasyPoi依赖

// 4.3.0版本在导出图片后会不能正常显示
		<dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.1.0</version>
        </dependency>
        <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>

实体构造

@Data
public class ExcelExportDto {
    // 注意type,imageType的取值
    @Excel(name = "图片",type = 2,imageType = 2,height = 50,width = 50)
    private byte[] pic;

    @Excel(name = "类型")
    private String type;

}

简单实现

    /**
     * 定义线程工厂,自定义线程名称
     */
    private static ThreadFactory testThreadFactory = new ThreadFactoryBuilder().setNameFormat("excel-pool-%d").build();

    /**
     * 定义线程池
     */
    private static ExecutorService executorService = new ThreadPoolExecutor(10,10,0L, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>(1024),testThreadFactory);

    @Override
    public void exportExcel(HttpServletResponse httpServletResponse) throws IOException {
        List<ExcelExportDto> excelExportDtos = new ArrayList<>();
        List<String> urlList = Arrays.asList("https://www.tencent.com/img/banners/brief-1-1.jpg", "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");
        // CountDownLatch:用于控制后续操作,等待线程全部结束后再进行
        CountDownLatch countDownLatch = new CountDownLatch(urlList.size());
        executorService.execute(() -> {
            for (String item : urlList) {
                System.out.println(JSON.toJSONString(item));
                ExcelExportDto u = new ExcelExportDto();
                // 网络图片需要转为byte[]
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                try {
                    URL url = new URL(item);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(2 * 1000);
                    // 通过输入流获取图片数据
                    InputStream inStream = conn.getInputStream();
                    // 得到图片的二进制数据
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = inStream.read(buffer)) != -1) {
                        outStream.write(buffer, 0, len);
                    }
                    inStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                u.setPic(outStream.toByteArray());
                u.setType("网络图片测试");
                excelExportDtos.add(u);
                countDownLatch.countDown();
                System.out.println("线程进行中");

            }
        });
        try {
            countDownLatch.await();
            System.out.println("线程全部结束啦");
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("网络图片测试", "网络图片测试", ExcelType.XSSF), ExcelExportDto.class, excelExportDtos);
            String fileName = "PicExcelExport";
            httpServletResponse.setCharacterEncoding("UTF-8");
            httpServletResponse.setHeader("content-Type", "application/vnd.ms-excel");
            httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            workbook.write(httpServletResponse.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

导出无需使用线程池,仅做测试
阿里开发规范—为何强制使用ThreadPoolExecutor创建线程池

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值