美文网首页
easypoi 导出网络图片,本地图片到excel

easypoi 导出网络图片,本地图片到excel

作者: zhy0324 | 来源:发表于2020-01-07 16:40 被阅读0次

    easypoi可以很方便的导出excel,但是涉及到图片的导出,还是有点麻烦的,尤其是获取网络图片然后导出excel
    思路是:获取网络图片为二进制字节数组,然后导出excel

    引入pom

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

    导出实体:

    package com.cec.park.module.dto;
    
    import cn.afterturn.easypoi.excel.annotation.Excel;
    
    /**
     * @author zhy
     * @title: TestDto
     * @projectName car_park
     * @description: TODO
     * @date 2019/10/2218:52
     */
    public class TestDto {
    
        @Excel(name = "名称",width = 40)
        private String name;
        @Excel(name = "公司LOGO", type = 2 ,width = 40 , height = 20,imageType = 2)
        private byte[] companyLogo;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public byte[] getCompanyLogo() {
            return companyLogo;
        }
    
        public void setCompanyLogo(byte[] companyLogo) {
            this.companyLogo = companyLogo;
        }
    }
    
    

    @Excel(name = "公司LOGO", type = 2 ,width = 40 , height = 20,imageType = 2)
    private byte[] companyLogo;

    type = 2 代表的是导出图片,imageType = 2 代表的是将二进制数组转换成图片。companyLogo是二进制数组

    如果是本地图片写法是
    @Excel(name = "公司LOGO", type = 2 ,width = 40 , height = 20,imageType = 1)
    private String companyLogo;
    companyLogo是本地图片路径

    获取网络图片为二进制数组的工具类

    package com.cec.park.module.utils;
    
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    /**
     * @author zhy
     * @title: HttpClientUtil
     * @projectName car_park
     * @description: http连接工具类
     * @date 2019/10/2219:23
     */
    public class HttpClientUtil {
    
    
        /**
         * @param [strUrl]
         * @return byte[]
         * @throws
         * @description: 获取网络图片转成字节流
         * @author zhy
         * @date 2019/10/23 8:59
         */
        public static byte[] getImageFromNetByUrl(String strUrl) {
            if (!isURL(strUrl)){
                return null;
            }
            try {
                URL url = new URL(strUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(2 * 1000);
                InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据
                byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据
                return btImg;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 从输入流中获取字节流数据
         *
         * @param inStream 输入流
         * @return
         * @throws Exception
         */
        public static byte[] readInputStream(InputStream inStream) throws Exception {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[10240];
            int len = 0;
            while ((len = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
            inStream.close();
            return outStream.toByteArray();
        }
    
        public static boolean isURL(String str) {
            str = str.toLowerCase();
            String regex = "^((https|http|ftp|rtsp|mms)?://)"
                    + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"
                    + "(([0-9]{1,3}\\.){3}[0-9]{1,3}"
                    + "|"
                    + "([0-9a-z_!~*'()-]+\\.)*"
                    + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\."
                    + "[a-z]{2,6})"
                    + "(:[0-9]{1,5})?"
                    + "((/?)|"
                    + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
            return str.matches(regex);
        }
    
    
    }
    
    
    

    导出方法

        @GetMapping("/exportTestExcel")
        public void exportTestExcel(HttpServletResponse response){
            List<TestDto> dtos = new ArrayList<>();
            TestDto testDto = new TestDto();
            testDto.setName("第一张");
            byte[] imageFromNetByUrl = HttpClientUtil.getImageFromNetByUrl("https://pics0.baidu.com/feed/79f0f736afc37931275e6caaa786164042a911c2.jpeg?token=5dfe37d5ec1518a0348aa50037f91067&s=3F654980420128EE65994110030050CA");
            testDto.setCompanyLogo(imageFromNetByUrl);
            dtos.add(testDto);
            String fileName = "测试.xls";
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("测试导出图片", "测试导出图片"), TestDto.class, dtos);
            ServletOutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
                response.setCharacterEncoding("UTF-8");
                response.setHeader("content-Type", "application/vnd.ms-excel");
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                workbook.write(outputStream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                if (outputStream != null){
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
    

    相关文章

      网友评论

          本文标题:easypoi 导出网络图片,本地图片到excel

          本文链接:https://www.haomeiwen.com/subject/tvxdactx.html