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 index, the index of the upper left row.
  • Upper left column index, the index of the upper left column.
  • Image file name, the name of the image file, complete with 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.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet 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 supports adding, extracting and manipulating OLE objects in worksheets. For this reason, Aspose.Cells 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 ImageData property specifies the image (icon) data of byte array type. The image will be displayed to show the OLE Object in the worksheet.
  • The ObjectData 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.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet.
Worksheet sheet = workbook.Worksheets[0];
// Define a string variable to store the image path.
string ImageUrl = dataDir + "logo.jpg";
// Get the picture into the streams.
FileStream fs = File.OpenRead(ImageUrl);
// Define a byte array.
byte[] imageData = new Byte[fs.Length];
// Obtain the picture into the array of bytes from streams.
fs.Read(imageData, 0, imageData.Length);
// Close the stream.
fs.Close();
// Get an excel file path in a variable.
string path = dataDir + "book1.xls";
// Get the file into the streams.
fs = File.OpenRead(path);
// Define an array of bytes.
byte[] objectData = new Byte[fs.Length];
// Store the file from streams.
fs.Read(objectData, 0, objectData.Length);
// Close the stream.
fs.Close();
// Add an Ole object into the worksheet with the image
// Shown in MS Excel.
sheet.OleObjects.Add(14, 3, 200, 220, imageData);
// Set embedded ole object data.
sheet.OleObjects[0].ObjectData = 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

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

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

// 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 = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];
// Add the line to the worksheet
sheet.Shapes.AddLine(2, 0, 2, 0, 100, 300);//method 1
//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

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.

// 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 = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];
// Add the line arrow to the worksheet
Shape s = sheet.Shapes.AddLine(2, 0, 2, 0, 100, 300);//method 1
//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.BeginArrowheadStyle = MsoArrowheadStyle.Arrow;//arrow type
s.Line.BeginArrowheadWidth = MsoArrowheadWidth.Wide;//arrow width
s.Line.BeginArrowheadLength = MsoArrowheadLength.Short;//arrow length
//add a arrow at the line end
s.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowOpen;//arrow type
s.Line.EndArrowheadWidth = MsoArrowheadWidth.Narrow;//arrow width
s.Line.EndArrowheadLength = MsoArrowheadLength.Long;//arrow length
//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

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

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

// 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 = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];
// Add the rectangle to the worksheet
sheet.Shapes.AddRectangle(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

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

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

// 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 = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];
// Add the cube to the worksheet
sheet.Shapes.AddAutoShape(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

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.

// 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 = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];
// Add the callout quad arrow to the worksheet
sheet.Shapes.AddAutoShape(AutoShapeType.QuadArrowCallout, 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

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.

// 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 = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];
// Add the multiplication sign to the worksheet
sheet.Shapes.AddAutoShape(AutoShapeType.MathMultiply, 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

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

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

// 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 = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];
// Add the multidocument to the worksheet
sheet.Shapes.AddAutoShape(AutoShapeType.FlowChartMultidocument, 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

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.

// 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 = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];
// Add the Five-pointed star to the worksheet
sheet.Shapes.AddAutoShape(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

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.

// 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 = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];
// Add the thought bubble cloud to the worksheet
sheet.Shapes.AddAutoShape(AutoShapeType.CloudCallout, 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