1. 程式人生 > >java 匯出Excel 轉圖片地址為圖片

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();
        }
    }