Excel加图片

Cell cell = rowData.createCell(di);
                               try {
                                   String v = this.getMapValue(r.get(data));
                                   File f = new File(downloadRoot+v);
                                   if(null!=v && !"".equals(v) && f.exists() && isDownImg) {


                                    BufferedImage _img = ImageIO.read(f);
                                    if(null!=_img) {
                                        String phone = this.getMapValue(r.get("phone"));

                                        if(phone.length()>=4) {
                                            phone="****"+phone.substring(4);
                                        }

                                        BufferedImage bufferImg= new BufferedImage(_img.getWidth(),_img.getHeight(),BufferedImage.TYPE_INT_RGB);
                                        bufferImg.getGraphics().drawImage(_img, 0, 0, null);

                                        bufferImg = ImageMarkUtil.getDrawNew(phone,bufferImg,Position.LOWER_RIGHT,Color.orange);


                                        double width = 70;
                                        double imageWidth = bufferImg.getWidth();
                                        double imageHeight = bufferImg.getHeight();
                                        double cellWidth = sheet.getColumnWidthInPixels(cell.getColumnIndex());
                                        double cellHeight = cell.getRow().getHeightInPoints() / 72 * 96;



                                        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
                                        ImageIO.write(bufferImg, "jpg", byteArrayOut);
                                        byte[] bytes = byteArrayOut.toByteArray();
                                        int pictureIdx = xssfWorkbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
                                        byteArrayOut.close();
                                        CreationHelper helper = xssfWorkbook.getCreationHelper();
                                        Drawing drawing = sheet.createDrawingPatriarch();
                                        ClientAnchor anchor = helper.createClientAnchor();
                                        // 图片存放- 列
                                        anchor.setCol1(di);
                                        // 图片存放- 行
                                        anchor.setRow1(i);
                                        Picture pict = drawing.createPicture(anchor, pictureIdx);
                                        if (imageWidth > width) {
                                            double scaleX = width / cellWidth;// 最终图片大小与单元格宽度的比例
                                            // 最终图片大小与单元格高度的比例
                                            // 计算方式:( imageHeight / imageWidth ) 是原图高于宽的比值,则 ( width * ( imageHeight / imageWidth ) ) 就是最终图片高的比值,
                                            // 那 ( width * ( imageHeight / imageWidth ) ) / cellHeight 就是所需比例了
                                            double scaleY = ( width * ( imageHeight / imageWidth ) ) / cellHeight;
                                            pict.resize(scaleX, scaleY);
                                        } else {
                                            pict.resize();
                                        }

                                       }
                                   }

                                   cell.setCellValue(v);

                            } catch (IOException e) {
                                e.printStackTrace();
                                cell.setCellValue(this.getMapValue(r.get(data)));
                            }
评论