EsayPOI导出Excel,包含图片

EsayPOI导出Excel,包含图片
最近遇到一个需求,需要导出数据库里某小区的住户的信息包含人脸图片。数据库里保存的是人脸图片的网络路径。
思路是:获取网络图片为二进制字节数组,然后导出excel
几个关键点:
1.将要导出的图片类型设为byte数组,Excel注解中 type = 2 代表的是导出图片,imageType = 2 代表的是将二进制数组转换成图片。
在这里插入图片描述
2.通过数据库中保存的网络图片路径获取图片的输入流,得到图片输入流的二进制数据赋值给实体中的图片字段。
EsayPOI导出Excel,包含图片_第1张图片

代码如下:
依赖



    cn.afterturn
    easypoi-base
    3.0.3


    cn.afterturn
    easypoi-web
    3.0.3


    cn.afterturn
    easypoi-annotation
    3.0.3

controller

@RequestMapping({Api.WEB + "/exportExcel"})
    public ResponseResult exportExcel() throws Exception {
        this.service.exportExcel(response);
        return resp;
    }

serviceImpl

 public void exportExcel(HttpServletResponse response) throws Exception {
        //获取需要导出的数据
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        List empHouseholdList = this.persEmpHouseholdExcelViewMapper.selectList(wrapper);//获取需要导出的数据
        for(PersEmpHouseholdExcelDto persEmpHouseholdExcelDto : empHouseholdList){
            List persBioTemplateList = this.persBioTemplateService.selectListByEmpId(persEmpHouseholdExcelDto.getId());//获取住户的人脸
            if(persBioTemplateList!=null && persBioTemplateList.size()>0){
                String urlStr = this.filServiceUrl + persBioTemplateList.get(0).getFaceImage();//图片网络路径
                persEmpHouseholdExcelDto.setUserBioTemplates(urlStr);//将图片地址也导出
                URL url = new URL(urlStr);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(2 * 1000);
                InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据
                byte[] btImg = readInputStream(inStream);// 得到图片输入流的二进制数据
                persEmpHouseholdExcelDto.setUserBioTemplatePage(btImg);
            }
        }
        //导出操作
        ExportParams params = new ExportParams();
        params.setTitle("住户人员数据导出");
        params.setSheetName("住户人员");
        Workbook workbook = ExcelExportUtil.exportExcel(params, PersEmpHouseholdExcelDto.class, empHouseholdList);
        Assert.isEmpty(workbook, "暂无数据");
        String fileName = "住户人员";
        this.OutputStream(response, workbook, fileName);
    }
	//导出操作
    private void OutputStream(HttpServletResponse response, Workbook workbook, String fileName) throws IOException {
        response.reset();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-msdownload;charset=UTF-8");// 设置类型
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment; filename=" + fileName + "_" + dateFormat.format(new Date()) + ".xls");
        response.setContentType("application/vnd.ms-excel");
        OutputStream output = response.getOutputStream();
        workbook.write(output);
        output.close();
    }
    /**
     * 从输入流中获取字节流数据
     *
     * @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();
    }

你可能感兴趣的:(EsayPOI导出Excel,包含图片)