Insert Pictures and Shapes of Excel files.

The shapes in excel are mainly divided into the following types:

  • Pictures
  • OleObjects
  • Lines
  • Rectangles
  • Basic Shapes
  • Block Arrows
  • Equation Shapes
  • FlowCharts
  • Stars and Banners
  • Callouts

This guide document will select one or two shapes from each type to make samples.Through these examples, you will learn how to use Aspose.Cells to insert the specified shape into the worksheet.

Adding Pictures in Excel Worksheet in C#

Adding pictures to a spreadsheet is very easy. It only takes a few lines of code: Simply call the add method of the pictures collection (encapsulated in the Worksheet object). The add method takes the following parameters:

  • upper_left_row, the index of the upper left row.
  • upper_left_column, the index of the upper left column.
  • file_name, the name of the image file, complete with path.
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")

Inserting OLE Objects into Excel Worksheet in C#

Aspose.Cells for Python via .NET supports adding, extracting and manipulating OLE objects in worksheets. For this reason, Aspose.Cells for Python via .NET has the OleObjectCollection class, used to add a new OLE Object to the collection list. Another class, OleObject, represents an OLE Object. It has some important members:

  • The image_data property specifies the image (icon) data of byte array type. The image will be displayed to show the OLE Object in the worksheet.
  • The object_data property specifies the object data in the form of a byte array. This data will be shown in its related program when you double-click on the OLE Object icon.

The following example shows how to add an OLE Object(s) into a worksheet.

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")

Inserting a Line to Excel Worksheet in C#

The shape of line belongs to the lines category.

In Microsoft Excel (for example 2007):

  • Select the cell where you want to insert the line
  • Click the Insert menu and click Shapes.
  • Then,select the line from ‘Recently Used Shapes’ or ‘Lines’

Using Aspose.Cells for Python via .NET

You can use the following method to insert a line in the worksheet.

The following example shows how to insert line to a worksheet.

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)

Execute the above code, you will get the following results:

Inserting a line arrow to Excel Worksheet in C#

The shape of line arrow belongs to the Lines category.It is a special case of line.

In Microsoft Excel (for example 2007):

  • Select the cell where you want to insert the line arrow
  • Click the Insert menu and click Shapes.
  • Then,select the line arrow from ‘Recently Used Shapes’ or ‘Lines’

Using Aspose.Cells for Python via .NET

You can use the following method to insert a line arrow in the worksheet.

The following example shows how to insert line arrow to a worksheet.

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)

Execute the above code, you will get the following results:

Inserting a Rectangle to Excel Worksheet in C#

The shape of rectangle belongs to the Rectangles category.

In Microsoft Excel (for example 2007):

  • Select the cell where you want to insert the rectangle
  • Click the Insert menu and click Shapes.
  • Then,select the rectangle from ‘Recently Used Shapes’ or ‘Rectangles’

Using Aspose.Cells for Python via .NET

You can use the following method to insert a rectangle in the worksheet.

The following example shows how to insert rectangle to a worksheet.

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)

Execute the above code, you will get the following results:

Inserting a Cube to Excel Worksheet in C#

The shape of cube belongs to the Basic Shapes category.

In Microsoft Excel (for example 2007):

  • Select the cell where you want to insert the cube
  • Click the Insert menu and click Shapes.
  • Then,select the Cube from Basic Shapes

Using Aspose.Cells for Python via .NET

You can use the following method to insert a cube in the worksheet.

The following example shows how to insert cube to a worksheet.

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)

Execute the above code, you will get the following results:

Inserting a callout quad arrow to Excel Worksheet in C#

The shape of callout quad arrow belongs to the Block Arrows category.

In Microsoft Excel (for example 2007):

  • Select the cell where you want to insert the callout quad arrow
  • Click the Insert menu and click Shapes.
  • Then,select the callout quad arrow from Block Arrows

Using Aspose.Cells for Python via .NET

You can use the following method to insert a callout quad arrow in the worksheet.

The following example shows how to insert callout quad arrow to a worksheet.

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)

Execute the above code, you will get the following results:

Inserting a multiplication sign to Excel Worksheet in C#

The shape of multiplication sign belongs to the Equation Shapes category.

In Microsoft Excel (for example 2007):

  • Select the cell where you want to insert the multiplication sign
  • Click the Insert menu and click Shapes.
  • Then,select the multiplication sign from Equation Shapes

Using Aspose.Cells for Python via .NET

You can use the following method to insert a multiplication sign in the worksheet.

The following example shows how to insert multiplication sign to a worksheet.

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)

Execute the above code, you will get the following results:

Inserting a multidocument to Excel Worksheet in C#

The shape of multidocument belongs to the FlowCharts category.

In Microsoft Excel (for example 2007):

  • Select the cell where you want to insert the multidocument
  • Click the Insert menu and click Shapes.
  • Then,select the multidocument from FlowCharts

Using Aspose.Cells for Python via .NET

You can use the following method to insert a multidocument in the worksheet.

The following example shows how to insert multidocument to a worksheet.

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)

Execute the above code, you will get the following results:

Inserting a Five-pointed star to Excel Worksheet in C#

The shape of Five-pointed star belongs to the Stars and Banners category.

In Microsoft Excel (for example 2007):

  • Select the cell where you want to insert the Five-pointed star
  • Click the Insert menu and click Shapes.
  • Then,select the Five-pointed star from Stars and Banners

Using Aspose.Cells for Python via .NET

You can use the following method to insert a Five-pointed star in the worksheet.

The following example shows how to insert Five-pointed star to a worksheet.

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)

Execute the above code, you will get the following results:

Inserting a thought bubble cloud to Excel Worksheet in C#

The shape of thought bubble cloud belongs to the Callouts category.

In Microsoft Excel (for example 2007):

  • Select the cell where you want to insert the thought bubble cloud
  • Click the Insert menu and click Shapes.
  • Then,select the thought bubble cloud from Callouts

Using Aspose.Cells for Python via .NET

You can use the following method to insert a thought bubble cloud in the worksheet.

The following example shows how to insert thought bubble cloud to a worksheet.

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)

Execute the above code, you will get the following results:

Advance topics