Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用ApachePOI获取xlsx文件中图片的位置_Java_Apache Poi_Xlsx - Fatal编程技术网

Java 如何使用ApachePOI获取xlsx文件中图片的位置

Java 如何使用ApachePOI获取xlsx文件中图片的位置,java,apache-poi,xlsx,Java,Apache Poi,Xlsx,我可以得到图片文件,但我不知道如何得到图片的位置。 我在POI中找不到正确的方法 InputStream is= new FileInputStream("test.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(is); List<PictureData> list = (List)workbook.getAllPictures(); int i = 0; System.out.pr

我可以得到图片文件,但我不知道如何得到图片的位置。 我在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++;
    }
InputStream is=新文件InputStream(“test.xlsx”);
XSSF工作簿=新XSSF工作簿(is);
列表=(列表)工作簿。getAllPictures();
int i=0;
System.out.println(list.size());
for(Iterator it=list.Iterator();it.hasNext();){
PictureData pd=it.next();
savePic(i,pd);
i++;
}
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
        }
    }
}
以下是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
        }
    }
}

还有另一种从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();         
                }

            }
        }

还有另一种从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();         
                }

            }
        }