当前位置: 首页 > 知识库问答 >
问题:

获取ShapeGroup中的所有形状

公羊伟志
2023-03-14
worksheet.getDrawingPatriarch().getShapes().get(0)

这将返回XSSFShapeGroup对象,但从这一点开始,我不知道如何在组中获得XSSFSimpleShape或XSSFShape。我想输出组中每个形状的位置(row&col),但我不知道如何从组中提取它们。

共有1个答案

谭昊乾
2023-03-14

到目前为止,使用apache poi无法从形状组(XSSFSimpleShapeXSSFShape)中获取XSSFShape)。

可以从CTGroupShape开始获取低级CTShapes。但是“输出该组中每个形状的位置(行和列)”几乎是不可能的,因为该组只有一个锚点(行,列),并且所有分组的形状都以单位emu(英国公制单位)相对于该锚点定位。

因此我们可以得到shape组的锚点以及组成员在emu中相对于那个锚点的位置:

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) {
  }
 }
}
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.ShapeContainer;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;

class ReadGroupedShapes {

 static void traverseShapeContainer(ShapeContainer<XSSFShape> container) {
  for (XSSFShape shape : container) { 
   // possible:   XSSFConnector, XSSFGraphicFrame, XSSFPicture, XSSFShapeGroup, XSSFSimpleShape
   if (shape instanceof XSSFConnector) {
    XSSFConnector connector = (XSSFConnector)shape;
    System.out.println(connector);

   } else if (shape instanceof XSSFGraphicFrame) {
    XSSFGraphicFrame graphicFrame = (XSSFGraphicFrame)shape;
    System.out.println(graphicFrame);

   } else if (shape instanceof XSSFPicture) {
    XSSFPicture picture = (XSSFPicture)shape;
    System.out.println(picture);

   } else if (shape instanceof XSSFShapeGroup) { //we have a shape group
    XSSFShapeGroup shapeGroup = (XSSFShapeGroup)shape;
    System.out.println(shapeGroup);

    traverseShapeContainer(shapeGroup); // we now can simply get the XSSFShapeGroup as ShapeContainer<XSSFShape>

   } else if (shape instanceof XSSFSimpleShape) {
    XSSFSimpleShape simpleShape = (XSSFSimpleShape)shape;
    System.out.println(simpleShape);

   }
  }

 }

 public static void main(String[] args) throws Exception {
  FileInputStream in = new FileInputStream("Shapegroup.xlsx");
  XSSFWorkbook wb = (XSSFWorkbook)WorkbookFactory.create(in);
  XSSFSheet sheet = wb.getSheetAt(0);
  XSSFDrawing drawing = sheet.getDrawingPatriarch();
  traverseShapeContainer(drawing);
 }
}
 类似资料:
  • 在jgrapht中,我添加了一些顶点。 我想知道如何获得所有我添加的顶点或jgrapht中已经存在的顶点? 有办法弄到吗?

  • 问题内容: 可以说我有一个Java程序包,其中包含所有继承自的类,我能以某种方式获得所有这些类吗?我正在锁定以下方面的东西: 这样有可能吗? 问题答案: 这是一个基本示例,假定类未打包JAR:

  • 问题内容: 我想获取某个类的对象的所有实例。 例如: 应该是或(顺序无关紧要)。 一个加号是,如果该函数将返回具有所请求类的超类的实例,尽管这不是必需的。 我能想到的一种方法是使用包含实例数组的静态类成员变量。在类的构造函数和析构函数中,我将在数组中添加或删除。如果我必须在许多类上这样做,这将很麻烦且容易出错。 问题答案: 如果您从TrackableObject类派生所有对象,则可以将此类设置为处

  • 问题内容: 获取Oracle中所有表的列表? 问题答案: 假设您有权访问DBA_TABLES数据字典视图。如果您没有这些特权但需要它们,则可以请求DBA显式授予您对该表的特权,或者请求DBA授予您该SELECT ANY DICTIONARY特权或SELECT_CATALOG_ROLE角色(这两者都将允许您查询任何数据字典表) )。当然,您可能希望排除某些模式,例如SYS和模式,SYSTEM而这些模

  • 问题内容: 是否有一种快速的方法可以从中的所有表获取所有列名,而不必列出所有表? 问题答案:

  • 问题内容: 我只是不明白一件事。当我输入时: 我得到: 但是选择似乎根本不起作用: 那怎么可能?我的意思是,表明存在一个名为“ db”的表… 其次,当我键入:它给我: 我能够做到: 我得到了一些结果。这是为什么?为什么我只能选择大写字母呢?而且,如何从仅以大写形式给出的表中进行选择?干杯 如果很重要:我以root用户身份登录到我的数据库。 问题答案: “ information_schema”仅具