7

Is it possible to extract an image's information from an xls spreadsheet using Apache POI?

In one of my projects, I need to read some images from a .xls file. I can read all images together, but how can I get images position (like columns and rows number or coordinates)? Otherwise I can get images position but I can't know information, like picture name or extension or others, about a specific image at the positions found. How I can get images and positions too?

Here read all images... and here get images positions...

4

3 に答える 3

3

このコードが役立つことを願っています)

    XSSFDrawing dp = workbook.getSheetAt(1).createDrawingPatriarch();
    List<XSSFShape> pics = dp.getShapes();
    XSSFPicture inpPic = (XSSFPicture)pics.get(0);

    XSSFClientAnchor clientAnchor = inpPic.getClientAnchor();
    inpPic.getShapeName(); // узнаю название картинки
    PictureData pict = inpPic.getPictureData();
    FileOutputStream out = new FileOutputStream("pict.jpg");
    byte[] data = pict.getData();
    out.write(data);
    out.close();
    System.out.println("col1: " + clientAnchor.getCol1() + ", col2: " + clientAnchor.getCol2() + ", row1: " + clientAnchor.getRow1() + ", row2: " + clientAnchor.getRow2());
    System.out.println("x1: " + clientAnchor.getDx1() + ", x2: " + clientAnchor.getDx2() +  ", y1: " + clientAnchor.getDy1() +  ", y2: " + clientAnchor.getDy2());
于 2018-09-04T15:40:06.690 に答える
0

また、イテレータを使用したくない場合 (重い場合があるため)

public List readDrawing(Workbook workbook) throws Exception {
    List list = workbook.getAllPictures();

    for (int i=0; i<list.size(); i++) {
        PictureData picture = (PictureData) list.get(i);

        String ext = picture.suggestFileExtension();
        byte[] data = picture.getData();

        FileOutputStream out = new FileOutputStream("imageName" + "." + ext);
        out.write(data);
        out.close();
    }

    return list;
}
于 2015-04-13T22:27:48.763 に答える