Read images from .xls file together with the references for their locations

后端 未结 1 408
别跟我提以往
别跟我提以往 2021-01-22 20:20

In one of my projects I need to read images from an .xls file. For each row there is a column containing an image which I need to read out.

It looks like

相关标签:
1条回答
  • 2021-01-22 20:52

    As long as the shapes are pictures, the following is possible:

    import org.apache.poi.hssf.usermodel.*;
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.ss.util.*;
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    class GetShapePosition {
    
     public static void main(String[] args) {
      try {
    
       InputStream inp = new FileInputStream("workbook.xls");
       Workbook wb = WorkbookFactory.create(inp);
    
       HSSFSheet sheet = (HSSFSheet)wb.getSheetAt(0);
    
       HSSFPatriarch dravingPatriarch = sheet.getDrawingPatriarch();
    
       java.util.List<HSSFShape> shapes = dravingPatriarch.getChildren();
    
       for (HSSFShape shape : shapes) {
        if (shape instanceof HSSFPicture) {
         HSSFPicture hssfPicture = (HSSFPicture)shape;
         int picIndex = hssfPicture.getPictureIndex();
         String filename = hssfPicture.getFileName();
         int row = hssfPicture.getClientAnchor().getRow1();
         int col = hssfPicture.getClientAnchor().getCol1();
         System.out.println("Picture " + picIndex + " with Filename: " + filename + " is located row: " + row + ", col: " + col);
        }
      }
    
      } catch (InvalidFormatException ifex) {
      } catch (FileNotFoundException fnfex) {
      } catch (IOException ioex) {
      }
     }
    }
    
    0 讨论(0)
提交回复
热议问题