1

画像ファイルは取得できますが、画像の場所を取得する方法がわかりません。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++;
    }
4

2 に答える 2

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();         
                }

            }
        }
于 2014-07-21T12:28:48.160 に答える
0

これが 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
        }
    }
}
于 2013-05-17T14:32:45.653 に答える