2016-09-29 5 views
0

Mein Code im Grunde wie folgt:Holen Sie sich alle Formen in ShapeGroup

worksheet.getDrawingPatriarch().getShapes().get(0) 

Dies wird das XSSFShapeGroup Objekt zurück, aber dann von diesem Punkt habe ich keine Ahnung, wie XSSFSimpleShape oder XSSFShape in der Gruppe zu erhalten. Ich möchte die Position (Zeile & col) jeder Form in dieser Gruppe ausgeben, aber ich weiß nicht, wie man sie aus der Gruppe extrahiert.

+0

Um 'XSSFSimpleShape' oder' XSSFShape' in der Gruppe ('XSSFShapeGroup') ist mit apache poi bisher nicht möglich. Es ** wäre ** möglich, die niedrige Stufe "CTShapes" ab der 'CTGroupShape' zu ​​bekommen. Aber "die Position (Zeile und Spalte) jeder Form in dieser Gruppe auszugeben" ist fast unmöglich, da es nur einen Anker (Zeile, Spalte) für die Gruppe gibt und alle gruppierten Formen relativ zu diesem Anker positioniert sind. –

+0

@AxelRichter danke, ich habe 'CTShapes' auch benutzt um Infos wie Name und Textinhalt zu bekommen, aber mit location. Danke trotzdem. –

Antwort

2

XSSFSimpleShape oder XSSFShape aus einer Formgruppe (XSSFShapeGroup) zu bekommen ist mit Apache Poi bis jetzt nicht möglich.

Es wäre möglich, die Low-Level-CTShapes von der CTGroupShape abzurufen. Aber "die Position (Zeile & Spalte) jeder Form in dieser Gruppe ausgeben" ist fast unmöglich, da es nur einen Anker (Zeile, Spalte) für die Gruppe gibt und alle gruppierten Formen relativ zu diesem Anker in Einheiten EMU positioniert sind (Englische metrische Einheiten).

So können wir den Anker der Formgruppe und die Position der Gruppenmitglieder in EMU relativ zu diesem Anker erhalten:

import org.apache.poi.ss.usermodel.*; 
import org.apache.poi.xssf.usermodel.*; 
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 

import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTConnector; 
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGraphicalObjectFrame; 
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture; 
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShape; 
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape; 
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor; 

import java.io.*; 

class ReadGroupedShapes { 

public static void main(String[] args) { 
    try { 

    InputStream inp = new FileInputStream("Shapegroup.xlsx"); 
    Workbook wb = WorkbookFactory.create(inp); 

    Sheet sheet = wb.getSheetAt(0); 

    XSSFDrawing drawing = (XSSFDrawing)sheet.getDrawingPatriarch(); 

    for (XSSFShape shape : drawing.getShapes()) { 
    // possible: XSSFConnector, XSSFGraphicFrame, XSSFPicture, XSSFShapeGroup, XSSFSimpleShape 
    if (shape instanceof XSSFConnector) { 
    //System.out.println(((XSSFConnector)shape).getCTConnector()); 

    } else if (shape instanceof XSSFGraphicFrame) { 
    //System.out.println(((XSSFGraphicFrame)shape).getCTGraphicalObjectFrame()); 

    } else if (shape instanceof XSSFPicture) { 
    //System.out.println(((XSSFPicture)shape).getCTPicture()); 

    } else if (shape instanceof XSSFShapeGroup) { //we have a shape group 
    //System.out.println(((XSSFShapeGroup)shape).getCTGroupShape()); 
    System.out.println("Whole group is anchored upper left:"); 
    int groupRow = ((XSSFClientAnchor)shape.getAnchor()).getRow1(); 
    long groupRowDy = ((XSSFClientAnchor)shape.getAnchor()).getDy1(); 
    System.out.print("Row: " + groupRow); 
    System.out.println(" + " + groupRowDy + " EMU"); 

    int groupCol = ((XSSFClientAnchor)shape.getAnchor()).getCol1(); 
    long groupColDx = ((XSSFClientAnchor)shape.getAnchor()).getDx1(); 
    System.out.print("Col: " + groupCol); 
    System.out.println(" + " + groupColDx + " EMU"); 

    //go through all shapes in the group 
    for (CTConnector lowLewelConnector : ((XSSFShapeGroup)shape).getCTGroupShape().getCxnSpList()) { 
     //System.out.println(lowLewelConnector); 
     System.out.println("A connector is in the group:"); 
     String cxnName = lowLewelConnector.getNvCxnSpPr().getCNvPr().getName(); 
     String cxnDescr = lowLewelConnector.getNvCxnSpPr().getCNvPr().getDescr(); 
     System.out.println("Name: " + cxnName + ((!"".equals(cxnDescr)) ? ": " + cxnDescr : "")); 
     System.out.println("positioned upper left:"); 
     long connectorDy = lowLewelConnector.getSpPr().getXfrm().getOff().getY(); 
     System.out.println("Row " + groupRow + " + " + groupRowDy + " + " + connectorDy + " EMU"); 

     long connectorDx = lowLewelConnector.getSpPr().getXfrm().getOff().getX(); 
     System.out.println("Col " + groupCol + " + " + groupColDx + " + " + connectorDx + " EMU"); 

    } 
    for (CTGraphicalObjectFrame lowLewelGrFrame : ((XSSFShapeGroup)shape).getCTGroupShape().getGraphicFrameList()) { 
     //System.out.println(lowLewelGrFrame); 

    } 
    for (CTPicture lowLewelPic : ((XSSFShapeGroup)shape).getCTGroupShape().getPicList()) { 
     //System.out.println(lowLewelPic); 
     System.out.println("A picture is in the group:"); 
     String picName = lowLewelPic.getNvPicPr().getCNvPr().getName(); 
     String picDescr = lowLewelPic.getNvPicPr().getCNvPr().getDescr(); 
     System.out.println("Name: " + picName + ((!"".equals(picDescr)) ? ": " + picDescr : "")); 
     System.out.println("positioned upper left:"); 
     long pictDy = lowLewelPic.getSpPr().getXfrm().getOff().getY(); 
     System.out.println("Row " + groupRow + " + " + groupRowDy + " + " + pictDy + " EMU"); 

     long pictDx = lowLewelPic.getSpPr().getXfrm().getOff().getX(); 
     System.out.println("Col " + groupCol + " + " + groupColDx + " + " + pictDx + " EMU"); 

    } 
    for (CTGroupShape lowLewelGroupShape : ((XSSFShapeGroup)shape).getCTGroupShape().getGrpSpList()) { 
     //System.out.println(lowLewelGroupShape); 

    } 
    for (CTShape lowLewelShape : ((XSSFShapeGroup)shape).getCTGroupShape().getSpList()) { 
     //System.out.println(lowLewelShape); 
     System.out.println("A simple shape is in the group:"); 
     String spName = lowLewelShape.getNvSpPr().getCNvPr().getName(); 
     String spDescr = lowLewelShape.getNvSpPr().getCNvPr().getDescr(); 
     System.out.println("Name: " + spName + ((!"".equals(spDescr)) ? ": " + spDescr : "")); 
     System.out.println("positioned upper left:"); 
     long simpleShapeDy = lowLewelShape.getSpPr().getXfrm().getOff().getY(); 
     System.out.println("Row " + groupRow + " + " + groupRowDy + " + " + simpleShapeDy + " EMU"); 

     long simpleShapeDx = lowLewelShape.getSpPr().getXfrm().getOff().getX(); 
     System.out.println("Col " + groupCol + " + " + groupColDx + " + " + simpleShapeDx + " EMU"); 

    } 

    } else if (shape instanceof XSSFSimpleShape) { 
    //System.out.println(((XSSFSimpleShape)shape).getCTShape()); 

    } 
    } 


    } catch (InvalidFormatException ifex) { 
    } catch (FileNotFoundException fnfex) { 
    } catch (IOException ioex) { 
    } 
} 
} 
+0

Wow, wirklich danke für deine Antwort. Obwohl es immer noch nicht die genaue Position bekommen kann, weil es noch keine Unterstützung gibt. Ich denke du hast die richtige Antwort. Nochmals vielen Dank. –

+0

Das Apache-POI-Team könnte sich freuen, Patches zu erhalten, die die Out-of-the-Box-Funktionen erweitern. – IceArdor

Verwandte Themen