当前位置:   article > 正文

java读取excel中的多图片_java读取excel单元格中的图片

java读取excel单元格中的图片

excel文件如图,获取张三的基本信息及图片信息。

代码如下:

需要导入poi相关的jar 版本3.9及以上
public class ReadExcel {
 
 public static void main(String[] args) throws Exception {
  InputStream inp = new FileInputStream("F:\\xxx.xls"); 
     HSSFWorkbook workbook = (HSSFWorkbook) WorkbookFactory.create(inp); 
        List<HSSFPictureData> pictures = workbook.getAllPictures(); 
        HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(0); 
        Map<String,Object> map = new HashMap<String,Object>();
       
     for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { 
            HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor(); 
         if (shape instanceof HSSFPicture) { 
                HSSFPicture pic = (HSSFPicture) shape; 
                int row = anchor.getRow2(); 
                int col = anchor.getCol2();
             System.out.println("--->" + anchor.getRow2() + ":"  + anchor.getCol2());
             //map.put(row+":"+col, row+":"+col);
             int pictureIndex = pic.getPictureIndex()-1; 
             HSSFPictureData picData = pictures.get(pictureIndex); 
             System.out.println("--->" + picData);
             map.put(row+":"+col, picData);
             savePic(UUID.randomUUID().toString(), picData); 
            } 
      }
    
     System.out.println(map);
 }
 
 private static void savePic(String i, PictureData pic) throws Exception { 
        String ext = pic.suggestFileExtension(); 
        byte[] data = pic.getData(); 
        if (ext.equals("jpeg")) { 
            FileOutputStream out = new FileOutputStream("F:\\" + i + ".jpg");
            out.write(data); 
            out.close(); 
         File file = new File("F:\\" + i + ".jpg");
         FileInputStream in = new FileInputStream(file);
         System.out.println("in===>"+in);
         if(file.isFile()){
          file.delete();
          System.out.println("=============delete");
         }
        
        } 
       /* if (ext.equals("png")) { 
            FileOutputStream out = new FileOutputStream("F:\\" + i + ".jpg"); 
            out.write(data); 
            out.close(); 
        }  */
 }
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/273102
推荐阅读
相关标签
  

闽ICP备14008679号