在Aspose.Cells中插入形状到工作表

Excel中的形状主要分为以下类型:

  • 线条
  • 矩形
  • 基本形状
  • 方块箭头
  • 方程式形状
  • 流程图
  • 星星和横幅
  • 标注

这份指南将从每个类型中选择一个或两个形状来制作示例。通过这些示例,您将学习如何使用Aspose.Cells将指定的形状插入工作表中。

向工作表插入一条线

线形状属于线条类别。

在Microsoft Excel中(例如2007年):

  • 选择要插入线条的单元格
  • 点击“插入”菜单,然后点击“形状”
  • 接着,从“最近使用的形状”或“线条”中选择线条

使用Aspose.Cells

您可以使用以下方法在工作表中插入线条。

下面的示例显示如何将线条插入工作表。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Create workbook from sample file
Workbook workbook = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.getWorksheets().get(0);
// Add the line to the worksheet
sheet.getShapes().addShape(MsoDrawingType.LINE, 2, 0, 2, 0, 100, 300);//method 1
//sheet.getShapes().addAutoShape(AutoShapeType.LINE, 2, 0, 2, 0, 100, 300);//method 2
//Save.You can check your icon in this way.
workbook.save("sample2.xlsx", SaveFormat.XLSX);

执行上述代码,您将获得以下结果:

向工作表插入带箭头的线

箭头线的形状属于线类别。它是线的特殊情况。

在Microsoft Excel中(例如2007年):

  • 选择要插入箭头线的单元格
  • 点击“插入”菜单,然后点击“形状”
  • 接着,从“最近使用的形状”或“线”中选择箭头线

使用Aspose.Cells

您可以使用以下方法在工作表中插入箭头线。

以下示例显示了如何向工作表插入箭头线。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Create workbook from sample file
Workbook workbook = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.getWorksheets().get(0);
// Add the line arrow to the worksheet
Shape s = sheet.getShapes().addShape(MsoDrawingType.LINE, 2, 0, 2, 0, 100, 300);//method 1
//Shape s = sheet.getShapes().addAutoShape(AutoShapeType.LINE, 2, 0, 2, 0, 100, 300);//method 2
//add a arrow at the line begin
s.getLine().setBeginArrowheadStyle(MsoArrowheadStyle.ARROW);//arrow type
s.getLine().setBeginArrowheadWidth(MsoArrowheadWidth.WIDE);//arrow width
s.getLine().setBeginArrowheadLength(MsoArrowheadLength.SHORT);//arrow length
//add a arrow at the line end
s.getLine().setEndArrowheadStyle(MsoArrowheadStyle.ARROW_OPEN);//arrow type
s.getLine().setEndArrowheadWidth(MsoArrowheadWidth.NARROW);//arrow width
s.getLine().setEndArrowheadLength(MsoArrowheadLength.LONG);//arrow length
//Save.You can check your icon in this way.
workbook.save("sample2.xlsx", SaveFormat.XLSX);

执行上述代码,您将获得以下结果:

向工作表插入矩形

矩形的形状属于矩形类别。

在Microsoft Excel中(例如2007年):

  • 选择要插入矩形的单元格
  • 点击“插入”菜单,然后点击“形状”
  • 接着,从“最近使用的形状”或“矩形”中选择矩形

使用Aspose.Cells

您可以使用以下方法在工作表中插入矩形。

以下示例显示了如何向工作表中插入矩形。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Create workbook from sample file
Workbook workbook = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.getWorksheets().get(0);
// Add the rectangle to the worksheet
ShapeCollection shapes = sheet.getShapes();
shapes.addShape(MsoDrawingType.RECTANGLE, 2, 0, 2, 0, 100, 300);
//Save.You can check your icon in this way.
workbook.save("sample2.xlsx", SaveFormat.XLSX);

执行上述代码,您将获得以下结果:

向工作表插入立体图

立方体的形状属于基本形状类别。

在Microsoft Excel中(例如2007年):

  • 选择要插入立方体的单元格
  • 点击“插入”菜单,然后点击“形状”
  • 然后,从基本形状中选择立方体

使用Aspose.Cells

您可以使用以下方法在工作表中插入立方体。

以下示例显示了如何向工作表中插入立方体。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Create workbook from sample file
Workbook workbook = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.getWorksheets().get(0);
// Add the cube to the worksheet
sheet.getShapes().addAutoShape(AutoShapeType.CUBE, 2, 0, 2, 0, 100, 300);
//Save.You can check your icon in this way.
workbook.save("sample2.xlsx", SaveFormat.XLSX);

执行上述代码,您将获得以下结果:

在工作表中插入箭头标签

呼叫四向箭头的形状属于块箭头类别。

在Microsoft Excel中(例如2007年):

  • 选择要插入标注四箭头的单元格
  • 点击“插入”菜单,然后点击“形状”
  • 然后,从块箭头中选择标注四箭头

使用Aspose.Cells

您可以使用以下方法在工作表中插入标注四箭头

以下示例显示了如何将标注四箭头插入工作表

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Create workbook from sample file
Workbook workbook = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.getWorksheets().get(0);
// Add the callout quad arrow to the worksheet
sheet.getShapes().addAutoShape(AutoShapeType.QUAD_ARROW_CALLOUT, 2, 0, 2, 0, 100, 100);
//Save.You can check your icon in this way.
workbook.save("sample2.xlsx", SaveFormat.XLSX);

执行上述代码,您将获得以下结果:

在工作表中插入乘号

乘法符号的形状属于方程形状类别

在Microsoft Excel中(例如2007年):

  • 选择要插入乘法符号的单元格
  • 点击“插入”菜单,然后点击“形状”
  • 然后,从方程形状中选择乘法符号

使用Aspose.Cells

您可以使用以下方法在工作表中插入乘法符号

以下示例显示如何将乘法符号插入工作表。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Create workbook from sample file
Workbook workbook = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.getWorksheets().get(0);
// Add the multiplication sign to the worksheet
sheet.getShapes().addAutoShape(AutoShapeType.MATH_MULTIPLY, 2, 0, 2, 0, 100, 100);
//Save.You can check your icon in this way.
workbook.save("sample2.xlsx", SaveFormat.XLSX);

执行上述代码,您将获得以下结果:

在工作表中插入多文档

多文档的形状属于流程图类别。

在Microsoft Excel中(例如2007年):

  • 选择要插入多文档的单元格
  • 点击“插入”菜单,然后点击“形状”
  • 然后,从流程图中选择多文档

使用Aspose.Cells

您可以使用以下方法在工作表中插入多文档。

以下示例显示如何向工作表中插入多文档。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Create workbook from sample file
Workbook workbook = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.getWorksheets().get(0);
// Add the multidocument to the worksheet
sheet.getShapes().addAutoShape(AutoShapeType.FLOW_CHART_MULTIDOCUMENT, 2, 0, 2, 0, 100, 100);
//Save.You can check your icon in this way.
workbook.save("sample2.xlsx", SaveFormat.XLSX);

执行上述代码,您将获得以下结果:

在工作表中插入五角星

五角星的形状属于星形图案类别。

在Microsoft Excel中(例如2007年):

  • 选择要插入五角星的单元格
  • 点击“插入”菜单,然后点击“形状”
  • 然后,从星形和横幅中选择五角星

使用Aspose.Cells

您可以使用以下方法在工作表中插入五角星

以下示例显示了如何向工作表中插入五角星。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Create workbook from sample file
Workbook workbook = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.getWorksheets().get(0);
// Add the Five-pointed star to the worksheet
sheet.getShapes().addAutoShape(AutoShapeType.STAR_5, 2, 0, 2, 0, 100, 100);
//Save.You can check your icon in this way.
workbook.save("sample2.xlsx", SaveFormat.XLSX);

执行上述代码,您将获得以下结果:

在工作表中插入思维气泡云

思维气泡云的形状属于标注类别。

在Microsoft Excel中(例如2007年):

  • 选择要插入思维气泡云的单元格
  • 点击“插入”菜单,然后点击“形状”
  • 然后,从标注中选择思维气泡云

使用Aspose.Cells

您可以使用以下方法在工作表中插入思维气泡云。

以下示例演示了如何向工作表中插入思维气泡云。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Create workbook from sample file
Workbook workbook = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.getWorksheets().get(0);
// Add the thought bubble cloud to the worksheet
sheet.getShapes().addAutoShape(AutoShapeType.CLOUD_CALLOUT, 2, 0, 2, 0, 100, 100);
//Save.You can check your icon in this way.
workbook.save("sample2.xlsx", SaveFormat.XLSX);

执行上述代码,您将获得以下结果: