How do I check if a cell contains an image using Apache POI?

你。 提交于 2020-01-23 02:26:11

问题


I have the following block of code:

        File file = new File("myFile.xlsx");  // my file
        inputStream = new FileInputStream(file);
        System.out.println("reading");
        XSSFWorkbook wb = new XSSFWorkbook(inputStream);
        XSSFSheet sh = wb.getSheetAt(0); // first sheet
        Iterator rowIter = sh.rowIterator();
        while(rowIter.hasNext()){ // iterate over all rows
            System.out.println("New Row "); // notify of new row
            Row myRow = (Row) rowIter.next();
            Iterator cellIter = myRow.cellIterator();
            while(cellIter.hasNext()){ // iterate over all cells in row
                XSSFCell myCell = (XSSFCell) cellIter.next();
                //how can I check that myCell has an image?
                //(I'm expecting it to be in the fourth cell each time)

                System.out.print(" " + myCell); // output cell content on same line
            }
        } 
        inputStream.close();

I am using Apache POI to read an .xlsx file. Each row can contain a cell with an image which was pasted into it by a user.

I'm hoping to grab the image and encode it to base64. How do I check if a cell contains an image while iterating through the spreadsheet?

I've already read http://poi.apache.org/spreadsheet/quick-guide.html#Images , but that deals with getting all images first, then iterating through them. I'd like to iterate through cells and check for images.


回答1:


The following code iterates through all images of the first sheet. So you don't need to iterate through rows/cols, but get the positions straight away. Apart of OneCellAnchor and TwoCellAnchor there are also AbsoluteAnchors which can't be directly associate with a cell.

import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.*;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.*;

public class Images2Cells {
    public static void main(String[] args) throws Exception {
        OPCPackage opc = OPCPackage.open("auto.xlsx", PackageAccess.READ);
        XSSFWorkbook book = new XSSFWorkbook(opc);

        XSSFSheet sheet = book.getSheetAt(0);
        for (POIXMLDocumentPart pdp : sheet.getRelations()) {
            if (!XSSFRelation.DRAWINGS.getRelation().equals(pdp.getPackageRelationship().getRelationshipType())) continue;

            PackagePart drawPP = pdp.getPackagePart();
            WsDrDocument draw = WsDrDocument.Factory.parse(drawPP.getInputStream());

            for (CTOneCellAnchor oneAnc : draw.getWsDr().getOneCellAnchorList()) {
                String picId = oneAnc.getPic().getBlipFill().getBlip().getEmbed();
                PackageRelationship pr = drawPP.getRelationship(picId);
                PackagePart imgPP = drawPP.getRelatedPart(pr);
                // byte imgBytes[] = IOUtils.toByteArray(imgPP.getInputStream());

                System.out.println(imgPP.getPartName()
                    +" - Col: "+oneAnc.getFrom().getCol()
                    +" - Row: "+oneAnc.getFrom().getRow()
                );
            }

            for (CTTwoCellAnchor twoAnc : draw.getWsDr().getTwoCellAnchorList()) {
                String picId = twoAnc.getPic().getBlipFill().getBlip().getEmbed();
                PackageRelationship pr = drawPP.getRelationship(picId);
                PackagePart imgPP = drawPP.getRelatedPart(pr);

                System.out.println(imgPP.getPartName()
                    +" - Col1: "+twoAnc.getFrom().getCol()
                    +" - Row1: "+twoAnc.getFrom().getRow()
                    +" - Col2: "+twoAnc.getTo().getCol()
                    +" - Row2: "+twoAnc.getTo().getRow()
                );
            }
        }

        opc.revert();
    }
}


来源:https://stackoverflow.com/questions/19280057/how-do-i-check-if-a-cell-contains-an-image-using-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!