Add Shapes to Excel worksheets in C# and VB.NET

With GemBox.Spreadsheet you can add shapes to your Excel worksheets programmatically in C# and VB.NET.

ExcelShape objects are visual illustrations inside the worksheet such as lines, curves, rectangles, and arrows. Shapes can be filled using the ExcelShape.Fill property and outlined using the ExcelShape.Outline property.

The ShapeType enumeration contains a list of built-in shape types directly supported through GemBox.Spreadsheet's API. Types that are not directly supported use ShapeType.Custom enum and are supported through preservation.

The following example shows how to create and add shapes to an Excel worksheet using C# and VB.NET.

Excel workbook with Shape elements
Screenshot of Excel file with shapes
using GemBox.Spreadsheet;
using GemBox.Spreadsheet.Drawing;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();
        var worksheet = workbook.Worksheets.Add("Shapes");

        var shape = worksheet.Shapes.Add(%ShapeType%, 100, 100, 200, 150, LengthUnit.Point);
        shape.Fill.SetSolid(DrawingColor.FromName(DrawingColorName.GreenYellow));
        shape.Outline.Fill.SetSolid(DrawingColor.FromName(DrawingColorName.DarkBlue));
        shape.Outline.Width = Length.From(3, LengthUnit.Point);

        var roundedRectangle = worksheet.Shapes.Add(ShapeType.RoundedRectangle, "B2", "D4");
        // Radius of the corners is 35% of the rounded rectangle height (since it is smaller than width).
        roundedRectangle.AdjustValues["adj"] = 35000;

        var rightArrow = worksheet.Shapes.Add(ShapeType.RightArrow, "B6", 100, 40, LengthUnit.Point);
        rightArrow.Fill.SetNone();
        rightArrow.Outline.Fill.SetSolid(DrawingColor.FromRgb(250, 30, 20));
        rightArrow.Outline.Width = Length.From(2, LengthUnit.Point);

        var line = worksheet.Shapes.Add(ShapeType.Line, "B12", "B15");
        line.Outline.Width = Length.From(10, LengthUnit.Pixel);

        workbook.Save("Shapes.%OutputFileType%");
    }
}
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.Drawing

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook As New ExcelFile()
        Dim worksheet = workbook.Worksheets.Add("Shapes")

        Dim shape = worksheet.Shapes.Add(%ShapeType%, 100, 100, 200, 150, LengthUnit.Point)
        shape.Fill.SetSolid(DrawingColor.FromName(DrawingColorName.GreenYellow))
        shape.Outline.Fill.SetSolid(DrawingColor.FromName(DrawingColorName.DarkBlue))
        shape.Outline.Width = Length.From(3, LengthUnit.Point)

        Dim roundedRectangle = worksheet.Shapes.Add(ShapeType.RoundedRectangle, "B2", "D4")
        ' Radius of the corners is 35% of the rounded rectangle height (since it is smaller than width).
        roundedRectangle.AdjustValues.Add("adj", 35000)

        Dim rightArrow = worksheet.Shapes.Add(ShapeType.RightArrow, "B6", 100, 40, LengthUnit.Point)
        rightArrow.Fill.SetNone()
        rightArrow.Outline.Fill.SetSolid(DrawingColor.FromRgb(250, 30, 20))
        rightArrow.Outline.Width = Length.From(2, LengthUnit.Point)

        Dim line = worksheet.Shapes.Add(ShapeType.Line, "B12", "B15")
        line.Outline.Width = Length.From(10, LengthUnit.Pixel)

        workbook.Save("Shapes.%OutputFileType%")
    End Sub
End Module

Shapes with rendering support

Note that GemBox.Spreadsheet's rendering engine doesn't support all shape types. Below is a list of ShapeType values that are currently supported when saving to PDF, XPS, or image formats:

  • Diamond
  • DownArrow
  • FlowchartDecision
  • FlowchartProcess
  • Hexagon
  • IsoscelesTriangle
  • LeftArrow
  • LeftRightArrow
  • Line
  • Octagon
  • Oval
  • OvalCallout
  • Parallelogram
  • Pentagon
  • Rectangle
  • RectangularCallout
  • RegularPentagon
  • RightArrow
  • RightTriangle
  • RoundedRectangle
  • RoundedRectangularCallout
  • SnipDiagonalCornerRectangle
  • SnipSameSideCornerRectangle
  • SnipSingleCornerRectangle
  • StraightConnector
  • Trapezoid
  • UpArrow
  • UpDownArrow

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy