插入Excel文件的图片和形状。

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

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

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

在C#中在Excel工作表中添加图片

向电子表格中添加图片非常简单。只需几行代码: 只需调用Worksheet中封装的pictures集合的add方法。add 方法接受以下参数:

  • upper_left_row,左上角行的索引。
  • upper_left_column,左上角列的索引。
  • file_name,图像文件的名称,含路径。
from aspose.cells import Workbook
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
sheetIndex = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[sheetIndex]
# Adding a picture at the location of a cell whose row and column indices
# Are 5 in the worksheet. It is "F6" cell
worksheet.pictures.add(5, 5, dataDir + "logo.jpg")
# Saving the Excel file
workbook.save(dataDir + "output.xls")

在C#中向Excel工作表插入OLE对象

Aspose.Cells for Python via .NET 支持在工作表中添加、提取和操作OLE对象。因此,Aspose.Cells for Python via .NET 具有 OleObjectCollection 类,用于向集合列表添加新的OLE对象。另一类 OleObject,代表OLE对象。它有一些重要成员:

  • image_data属性指定图像(图标)数据为字节数组类型。将显示该图像以显示工作表中的OLE对象。
  • object_data属性指定对象数据的字节数组形式。当您双击OLE对象图标时,将在其关联的程序中显示此数据。

下面的示例演示了如何将一个或多个OLE对象添加到工作表中。

from aspose.cells import Workbook
from os import os, path
import bytearray
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiate a new Workbook.
workbook = Workbook()
# Get the first worksheet.
sheet = workbook.worksheets[0]
# Define a string variable to store the image path.
ImageUrl = dataDir + "logo.jpg"
# Get the picture into the streams.
fs = open(ImageUrl, "rb")
# Define a byte array.
imageData = bytearray(utils.filesize(fs))
# Obtain the picture into the array of bytes from streams.
fs.readinto(imageData)
# Close the stream.
fs.close()
# Get an excel file path in a variable.
path = dataDir + "book1.xls"
# Get the file into the streams.
fs = open(path, "rb")
# Define an array of bytes.
objectData = bytearray(utils.filesize(fs))
# Store the file from streams.
fs.readinto(objectData)
# Close the stream.
fs.close()
# Add an Ole object into the worksheet with the image
# Shown in MS Excel.
sheet.ole_objects.add(14, 3, 200, 220, imageData)
# Set embedded ole object data.
sheet.ole_objects[0].object_data = objectData
# Save the excel file
workbook.save(dataDir + "output.out.xls")

在C#中向Excel工作表插入线条

线形状属于线条类别。

在Microsoft Excel中(例如2007年):

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

使用Aspose.Cells for Python via .NET

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

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

from aspose.cells import SaveFormat, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create workbook from sample file
workbook = Workbook()
# Access first worksheet from the collection
sheet = workbook.worksheets[0]
# Add the line to the worksheet
sheet.shapes.add_line(2, 0, 2, 0, 100, 300)
# sheet.Shapes.AddAutoShape(AutoShapeType.Line, 2, 0, 2, 0, 100, 300);# method 2
# sheet.Shapes.AddShape(MsoDrawingType.Line, 2, 0, 2, 0, 100, 300);# method 3
# Save.You can check your line in this way.
workbook.save("sample.xlsx", SaveFormat.XLSX)

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

在C#中向Excel工作表插入线箭头

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

在Microsoft Excel中(例如2007年):

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

使用Aspose.Cells for Python via .NET

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

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

from aspose.cells import SaveFormat, Workbook
from aspose.cells.drawing import MsoArrowheadLength, MsoArrowheadStyle, MsoArrowheadWidth
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create workbook from sample file
workbook = Workbook()
# Access first worksheet from the collection
sheet = workbook.worksheets[0]
# Add the line arrow to the worksheet
s = sheet.shapes.add_line(2, 0, 2, 0, 100, 300)
# Shape s = sheet.Shapes.AddAutoShape(AutoShapeType.Line, 2, 0, 2, 0, 100, 300);# method 2
# Shape s = sheet.Shapes.AddShape(MsoDrawingType.Line, 2, 0, 2, 0, 100, 300);# method 3
# add a arrow at the line begin
s.line.begin_arrowhead_style = MsoArrowheadStyle.ARROW
s.line.begin_arrowhead_width = MsoArrowheadWidth.WIDE
s.line.begin_arrowhead_length = MsoArrowheadLength.SHORT
# add a arrow at the line end
s.line.end_arrowhead_style = MsoArrowheadStyle.ARROW_OPEN
s.line.end_arrowhead_width = MsoArrowheadWidth.NARROW
s.line.end_arrowhead_length = MsoArrowheadLength.LONG
# Save.You can check your arrow in this way.
workbook.save("sample.xlsx", SaveFormat.XLSX)

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

在 C# 中向 Excel 工作表插入矩形

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

在Microsoft Excel中(例如2007年):

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

使用Aspose.Cells for Python via .NET

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

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

from aspose.cells import SaveFormat, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create workbook from sample file
workbook = Workbook()
# Access first worksheet from the collection
sheet = workbook.worksheets[0]
# Add the rectangle to the worksheet
sheet.shapes.add_rectangle(2, 0, 2, 0, 100, 300)
# Save
workbook.save("sample.xlsx", SaveFormat.XLSX)

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

在 C# 中向 Excel 工作表插入立方体

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

在Microsoft Excel中(例如2007年):

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

使用Aspose.Cells for Python via .NET

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

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

from aspose.cells import SaveFormat, Workbook
from aspose.cells.drawing import AutoShapeType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create workbook from sample file
workbook = Workbook()
# Access first worksheet from the collection
sheet = workbook.worksheets[0]
# Add the cube to the worksheet
sheet.shapes.add_auto_shape(AutoShapeType.CUBE, 2, 0, 2, 0, 100, 300)
# Save.You can check your cube in this way.
workbook.save("sample.xlsx", SaveFormat.XLSX)

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

在C#中向Excel工作表中插入标注四方箭头

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

在Microsoft Excel中(例如2007年):

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

使用Aspose.Cells for Python via .NET

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

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

from aspose.cells import SaveFormat, Workbook
from aspose.cells.drawing import AutoShapeType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create workbook from sample file
workbook = Workbook()
# Access first worksheet from the collection
sheet = workbook.worksheets[0]
# Add the callout quad arrow to the worksheet
sheet.shapes.add_auto_shape(AutoShapeType.QUAD_ARROW_CALLOUT, 2, 0, 2, 0, 100, 100)
# Save
workbook.save("sample.xlsx", SaveFormat.XLSX)

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

在C#中向Excel工作表插入乘号的形状

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

在Microsoft Excel中(例如2007年):

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

使用Aspose.Cells for Python via .NET

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

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

from aspose.cells import SaveFormat, Workbook
from aspose.cells.drawing import AutoShapeType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create workbook from sample file
workbook = Workbook()
# Access first worksheet from the collection
sheet = workbook.worksheets[0]
# Add the multiplication sign to the worksheet
sheet.shapes.add_auto_shape(AutoShapeType.MATH_MULTIPLY, 2, 0, 2, 0, 100, 100)
# Save.You can check your multiplication in this way.
workbook.save("sample.xlsx", SaveFormat.XLSX)

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

在C#中向Excel工作表插入多文档的形状

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

在Microsoft Excel中(例如2007年):

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

使用Aspose.Cells for Python via .NET

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

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

from aspose.cells import SaveFormat, Workbook
from aspose.cells.drawing import AutoShapeType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create workbook from sample file
workbook = Workbook()
# Access first worksheet from the collection
sheet = workbook.worksheets[0]
# Add the multidocument to the worksheet
sheet.shapes.add_auto_shape(AutoShapeType.FLOW_CHART_MULTIDOCUMENT, 2, 0, 2, 0, 100, 100)
# Save
workbook.save("sample.xlsx", SaveFormat.XLSX)

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

在C#中向Excel工作表插入五角星

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

在Microsoft Excel中(例如2007年):

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

使用Aspose.Cells for Python via .NET

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

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

from aspose.cells import SaveFormat, Workbook
from aspose.cells.drawing import AutoShapeType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create workbook from sample file
workbook = Workbook()
# Access first worksheet from the collection
sheet = workbook.worksheets[0]
# Add the Five-pointed star to the worksheet
sheet.shapes.add_auto_shape(AutoShapeType.STAR5, 2, 0, 2, 0, 100, 100)
# Save.You can check your icon in this way.
workbook.save("sample.xlsx", SaveFormat.XLSX)

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

在C#中向Excel工作表插入一个思维气泡云

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

在Microsoft Excel中(例如2007年):

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

使用Aspose.Cells for Python via .NET

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

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

from aspose.cells import SaveFormat, Workbook
from aspose.cells.drawing import AutoShapeType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Create workbook from sample file
workbook = Workbook()
# Access first worksheet from the collection
sheet = workbook.worksheets[0]
# Add the thought bubble cloud to the worksheet
sheet.shapes.add_auto_shape(AutoShapeType.CLOUD_CALLOUT, 2, 0, 2, 0, 100, 100)
# Save
workbook.save("sample.xlsx", SaveFormat.XLSX)

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

高级主题