使用形状

使用形状

Aspose.Cells FOSS for Java 让您能够添加和管理锚定到工作表单元格的绘图对象(自动形状)。形状通过 ShapeCollection 暴露,可通过 Worksheet.getShapes() 访问。每个形状都有几何类型、自动形状类型、名称以及双单元格锚定位置。


概述

工作表中的形状通过 ShapeCollection 管理。您可以使用 ShapeCollection.add(autoShapeType, upperLeftRow, upperLeftRowOffset, upperLeftColumn, upperLeftColumnOffset, height, width) 添加新形状,该方法返回新形状的零基索引。传入 AutoShapeType 值以指定预设几何形状。

形状加入集合后,您可以读取和写入其名称、几何类型、自动形状类型以及锚定单元格位置。


关键类

描述
Shape表示具有名称、几何形状和单元格锚点位置的绘图对象(自动形状)。
ShapeCollection工作表上所有绘图形状的集合。支持计数、索引访问、添加和删除。
AutoShapeType枚举,指定形状的预设 DrawingML 几何形状。
ShapeModel形状的内部序列化模型。公开原始 XML 字段,以供高级往返使用。

添加形状

使用 ShapeCollection.add(...) 插入绘图对象。该方法接受:

  • 用于几何预设的 AutoShapeType 值。
  • 左上锚点(行、行偏移、列、列偏移)——全部为零基整数。
  • 形状的高度和宽度(像素)。

该方法返回新形状的零基索引。


读取形状属性

对于一个 Shape 对象,可使用以下读取方法:

  • Shape.getName() — 形状的内部名称字符串。
  • Shape.getGeometryType() — 几何类型字符串。
  • Shape.getAutoShapeType()AutoShapeType 枚举值。
  • Shape.getUpperLeftRow() / Shape.getUpperLeftColumn() — 左上角锚点单元格。
  • Shape.getLowerRightRow() / Shape.getLowerRightColumn() — 右下角锚点单元格。

编写形状属性

  • Shape.setName(name) — 重命名形状。
  • Shape.setGeometryType(type) — 更改几何类型字符串。
  • Shape.setAutoShapeType(type) — 更改 AutoShapeType 预设。
  • Shape.setUpperLeftRow(row) / Shape.setUpperLeftColumn(col) — 重新定位。
  • Shape.setLowerRightRow(row) / Shape.setLowerRightColumn(col) — 调整大小锚点。

API Reference 摘要

Shape

方法返回类型描述
getName()String形状的内部名称。
setName(name)void设置内部名称。
getGeometryType()String几何类型标识符字符串。
setGeometryType(type)void设置几何类型字符串。
getAutoShapeType()AutoShapeType预设自动形状几何枚举值。
setAutoShapeType(type)void设置预设自动形状几何。
getUpperLeftRow()int左上角锚点单元格的行索引。
setUpperLeftRow(row)void设置左上角锚点行。
getUpperLeftColumn()int左上角锚点单元格的列索引。
setUpperLeftColumn(col)void设置左上角锚点列。
getLowerRightRow()int底部右侧锚点单元格的行索引。
setLowerRightRow(row)void设置右下角锚点行。
getLowerRightColumn()int右下角锚点单元格的列索引。
setLowerRightColumn(col)void设置右下角锚点列。

ShapeCollection

方法返回类型描述
getCount()int工作表上的形状数量。
get(index)Shape返回给定零基索引处的形状。
add(...)int添加一个新形状;返回其在集合中的索引。
removeAt(index)void移除给定索引处的形状。

另请参阅

 中文