【问题标题】:How can I get the location of picture in a xlsx file by using Apache POI如何使用 Apache POI 获取 xlsx 文件中图片的位置
【发布时间】:2013-04-21 12:48:10
【问题描述】:

我可以获取图片文件,但是我不知道如何获取图片的位置。 我在 POI 中找不到正确的方法。

    InputStream is= new FileInputStream("test.xlsx");  
    XSSFWorkbook workbook = new XSSFWorkbook(is);  

    List<PictureData> list = (List)workbook.getAllPictures();
    int i = 0;
    System.out.println(list.size());
    for(Iterator<PictureData> it = list.iterator(); it.hasNext(); ){
        PictureData pd = it.next();
        savePic(i, pd);
        i++;
    }

【问题讨论】:

    标签: java apache-poi xlsx


    【解决方案1】:

    还有另一种方法可以从 XSSFSheet 获取位置信息。

            XSSFSheet sheet = workBook.getSheetAt(0);
            XSSFDrawing drawing = sheet.createDrawingPatriarch(); // I know it is ugly, actually you get the actual instance here
            for (XSSFShape shape : drawing.getShapes()) {
                if (shape instanceof XSSFPicture) {
                    XSSFPicture picture = (XSSFPicture) shape;
                    XSSFPictureData xssfPictureData = picture.getPictureData();
                    ClientAnchor anchor = picture.getPreferredSize();
                    int row1 = anchor.getRow1();
                    int row2 = anchor.getRow2();
                    int col1 = anchor.getCol1();
                    int col2 = anchor.getCol2();
                    System.out.println("Row1: " + row1 + " Row2: " + row2);
                    System.out.println("Column1: " + col1 + " Column2: " + col2);
                    // Saving the file
                    String ext = xssfPictureData.suggestFileExtension();                
                    byte[] data = xssfPictureData.getData();
                    String filePath = "C:\\..\\Images\\" + xssfPictureData.getPackagePart().getPartName();
                    try (FileOutputStream os = new FileOutputStream(filePath)) {
                        os.write(data);
                        os.flush();         
                    }
    
                }
            }
    

    【讨论】:

      【解决方案2】:

      getAllPictures() 的实现方式如下:使用 getRelations() 查询 XSSFSheet (inSheet) 并查找 XSSFDrawing。

      CTDrawing 类提供了几种获取图片锚点的方法。

      for(POIXMLDocumentPart dr : inSheet.getRelations()) {
          if(dr instanceof XSSFDrawing) {
              CTDrawing ctDrawing = ((XSSFDrawing) dr).getCTDrawing();
              for (CTTwoCellAnchor anchor: ctDrawing.getTwoCellAnchorList()) {
                  CTMarker from = anchor.getFrom();
                  int row1 = from.getRow();
                  int col1 = from.getCol();
      
                  CTMarker to = anchor.getTo();
                  int row2 = to.getRow();
                  int col2 = to.getCol();
      
                  // do something here
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-30
        相关资源
        最近更新 更多