POI读取Excel2007中的图片

FileInputStream inputStream = new FileInputStream(filePath);
XSSFWorkbook  workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
for (POIXMLDocumentPart dr : sheet.getRelations()) {
     if (dr instanceof XSSFDrawing) { 
         XSSFDrawing drawing = (XSSFDrawing) dr;  
               List shapes = drawing.getShapes();  
               for (XSSFShape shape : shapes) {
                XSSFPicture pic = (XSSFPicture) shape;
                XSSFClientAnchor anchor = null;
                try {
                    anchor = pic.getPreferredSize();//获取图片
                    anchor.setCol1(4);
                    anchor.setRow1(2);
                    anchor.setCol2(8);
                    anchor.setRow2(6);
                } catch (Exception e) {
                    //anchor = pic.getPreferredSize();//会出现异常
                    //Excel中的图片存在,但是图片地址或者图片损坏,会报异常,所以需要过滤这些 图片。
                    continue;
                }
               }  
     }
}

你可能感兴趣的:(Java)