hutool excel导出图片链接处理

/**
  * 
  * @param exportTemplateList 需要导出的数据
  * @param writer 
  * @param orderStrMap 字段对应在哪列
*/
private static void processImagelink(List exportTemplateList, ExcelWriter writer, Map<String, String> orderStrMap) {
        if (CollectionUtil.isNotEmpty(exportTemplateList)) {
            List<Map<String, Object>> exportData = exportTemplateList;
            for (int i = 0; i < exportData.size(); i++) {
                Map<String, Object> map = exportData.get(i);
                Iterator<Map.Entry<String, Object>> objectIterator = map.entrySet().iterator();
                while (objectIterator.hasNext()) {
                    Map.Entry<String, Object> objectEntry = objectIterator.next();
                    String value = objectEntry.getValue().toString();
                    if (value.toLowerCase().contains(".jpg") || value.toLowerCase().contains(".jpeg") || value.toLowerCase().contains(".png")) {
                        int num = Integer.parseInt(orderStrMap.get(objectEntry.getKey()));
                        List<String> imgUrls = GSONUtil.toList(value, String.class);

                        for (int img = 0; img < imgUrls.size(); img++) {
                            BufferedImage bufferedImage = ImgUtil.read(URLUtil.url(imgUrls.get(img)));
                            Image image = ImgUtil.scale(bufferedImage, 0.25f);

                            byte[] pictureData = ImgUtil.toBytes(image, "jpg");


                            // 写入图片
                            writePic(writer, num, i + 1, pictureData, HSSFWorkbook.PICTURE_TYPE_JPEG, img + 1, imgUrls.size());
                            writer.autoSizeColumnAll();
                        }
                    }
                }
            }

        }
    }

    /**
     * @param writer
     * @param x           单元格x轴坐标
     * @param y           单元格y轴坐标
     * @param pictureData 图片二进制数据
     * @param picType     图片格式
     */
    private static void writePic(ExcelWriter writer, int x, int y, byte[] pictureData, int picType, int img, int size) {
      

        Sheet sheet = writer.getSheet();
        Drawing drawingPatriarch = sheet.createDrawingPatriarch();
        // 设置图片单元格位置


        ClientAnchor anchor = drawingPatriarch.createAnchor(0, 0, 0, 0, x, y, x + 1, y + 1);

        // 随单元格改变位置和大小
        anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
        // 添加图片
        int pictureIndex = sheet.getWorkbook().addPicture(pictureData, picType);

        Picture picture = drawingPatriarch.createPicture(anchor, pictureIndex);

    }

你可能感兴趣的:(excel)