最近搜索

从excel 获取图片。和位置。

浏览:740
管理员 2022-11-20 14:51



 //从03格式excel中获取图片
    private static void getSheetPictures03(File userUploadFile ) throws InvalidFormatException, IOException {
       //创建Workbook
       POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(userUploadFile));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet =    wb.getSheetAt(0);
        List pictures = wb.getAllPictures();
        System.out.println("图片数量:"+pictures.size());
        if (pictures.size() != 0) {
            for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
                HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
                if (shape instanceof HSSFPicture) {
                    HSSFPicture pic = (HSSFPicture) shape;
                    System.out.println("第几列:"+anchor.getCol1());
                    System.out.println("第几行:"+anchor.getRow1());
                    System.out.println(pic.getPictureData().suggestFileExtension());
                    System.out.println(pic.getPictureData().getMimeType());
                    InputStream inputStream = new ByteArrayInputStream(pic.getPictureData().getData());
                    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                  //创建一个Buffer字符串
                  byte[] buffer = new byte[1024];
                  //每次读取的字符串长度,如果为-1,代表全部读取完毕
                  int len = 0;
                  //使用一个输入流从buffer里把数据读取出来
                  while( (len=inputStream.read(buffer)) != -1 ){
                 //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
                 outStream.write(buffer, 0, len);
                  }
                  //关闭输入流
                  inputStream.close();
                  //把outStream里的数据写入内存
                  //得到图片的二进制数据,以二进制封装得到数据,具有通用性
                  byte[] data = outStream.toByteArray();
                  //new一个文件对象用来保存图片,默认保存当前工程根目录
                  File imageFile = new File("E:/BeautyGirl.jpg");
                  //创建输出流
                  FileOutputStream fileOutStream = new FileOutputStream(imageFile);
                  //写入数据
                  fileOutStream .write(data);
                  fileOutStream.close();
                    //.suggestFileExtension() 输出 jpeg
                    //getMimeType() 输出  image/jpeg
                    //positionEntity.setSheetNum(sheetNum);
                    //positionEntity.setColumn((int) anchor.getCol1());
                    //positionEntity.setRow(anchor.getRow1());
                    //positionEntity.setPictureData(pic.getPictureData());
                }
            }
        }
    }



  /**
    * 获取图片和位置 (xlsx)  不是03格式
    * @return
    * @throws IOException
    */
   public static Map<String, PictureData> getPictures2(MultipartFile file  ) throws IOException {
      InputStream fs = file.getInputStream();
      XSSFWorkbook wb = new XSSFWorkbook(fs);
      XSSFSheet sheet1 =  wb.getSheetAt(0);
      Map<String, PictureData> map = new HashMap<String, PictureData>();
      List<POIXMLDocumentPart> list = sheet1.getRelations();
      for (POIXMLDocumentPart part : list) {
         if (part instanceof XSSFDrawing) {
            XSSFDrawing drawing = (XSSFDrawing) part;
            List<XSSFShape> shapes = drawing.getShapes();
            for (XSSFShape shape : shapes) {
               XSSFPicture picture = (XSSFPicture) shape;
               XSSFClientAnchor anchor = picture.getPreferredSize();
               CTMarker marker = anchor.getFrom();
               int rowNum = marker.getRow();
               int colNum = marker.getCol();
               String key = marker.getRow() + "";
//                    String key = marker.getRow() + "-" + marker.getCol();
               System.out.println("行是:"+rowNum+"列是:"+colNum);
               if(colNum==1){
                  if(picture.getPictureData().getData().length<120*1024){
                     map.put(key, picture.getPictureData());
                  }else{
                     // 主图大小不符合要求
                     System.out.println(String.format("{}行{}列主图大小不符合要求",rowNum,colNum));
                  }
                  map.put(key, picture.getPictureData());
               }
            }
         }
      }
      return map;
   }
联系站长

站长微信:xiaomao0055

站长QQ:14496453