Bootstrap

java poi 导出图片

@GetMapping("/export")
public static void exportWithPic(HttpServletResponse response) {
    // 创建一个excel文档对象
    HSSFWorkbook wb = new HSSFWorkbook();
    try {
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        URL url = new URL("https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20190127%2Fab1674ad76ea4b61b25164600493e472.jpeg&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg");
        BufferedImage bufferImg = ImageIO.read(url);
        ImageIO.write(bufferImg, "jpg", byteArrayOut);
        byteArrayOut.close();

        HSSFSheet sheet = wb.createSheet("picture");
        int columnWidth = sheet.getColumnWidth(8);
        sheet.setColumnWidth(7,  (int)(columnWidth * 2.25));
        //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)i
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        //anchor主要用于设置图片的属性
        HSSFRow hssfRow = sheet.createRow(0);
        HSSFCell cell = hssfRow.createCell(7);
        cell.setCellValue("图片");
        short column = 7;
        for (int row = 1; row < 10; row++){
            for(int i = 1, hoffset = 255, woffset = (int) (256 / 2.25); i < 10; i++){
                try {
                    HSSFClientAnchor anchor = new HSSFClientAnchor((i - 1)*woffset, 0, i * woffset, hoffset, column, row, column, row);
                    anchor.setAnchorType(3);
                    //插入图片
                    patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
                }catch (Exception e){

                }
            }
        }
        response.setHeader("Content-Disposition", "attachment;Filename=picture-" + DateUtils.getNowTime(DateUtils.formatStr_yyyyMMddHHmmss) + ".xlsx");
        response.setCharacterEncoding("utf-8");
        OutputStream os = response.getOutputStream();
        wb.write(os);
        os.flush();
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
    }
}
;