java 导出Excel 转图片地址为图片

drawPictureInfoExcel(wb, patriarch, i + 1, bean.getPicture1());//i+1代表当前的行


  private void drawPictureInfoExcel(HSSFWorkbook wb, HSSFPatriarch patriarch, int rowIndex, String pictureUrl) {
        try {
            //anchor主要用于设置图片的属性  
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 40, 40, (short) 1, rowIndex, (short) 1, rowIndex);
            //Sets the anchor type (图片在单元格的位置)
            //0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
            anchor.setAnchorType(0);
            URL url = new URL(pictureUrl);
            BufferedImage bufferImg = ImageIO.read(url);
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
            byte[] data = byteArrayOut.toByteArray();
            patriarch.createPicture(anchor, wb.addPicture(data, HSSFWorkbook.PICTURE_TYPE_JPEG));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


你可能感兴趣的:(java)