Skip to content
Mats Alm edited this page Nov 3, 2023 · 20 revisions

Shapes

Supports adding 187 different types of shapes with multiple ways of formatting.

Add a Shape (textbox)

var shape = ws.Drawings.AddShape("txtDesc", eShapeStyle.Rect);
shape.SetPosition(1, 5, 6, 5);  //Position Row, RowOffsetPixels, Column, ColumnOffsetPixels
shape.SetSize(400, 200);        //Size in pixels
shape.EditAs = eEditAs.Absolute;
shape.Text = "This example demonstrates how to create various drawing objects like pictures, shapes and charts.\n\r\n\rThe first sheet contains all subdirectories and files with an icon, name, size and dates.\r\n\r\nThe second sheet contains statistics about extensions and the top-10 largest files.";
shape.Fill.Style = eFillStyle.SolidFill;
shape.Fill.Color = Color.DarkSlateGray;
shape.Fill.Transparancy = 20;
shape.TextAnchoring = eTextAnchoringType.Top;
shape.TextVertical = eTextVerticalType.Horizontal;
shape.TextAnchoringControl=false;

shape.Effect.SetPresetShadow(ePresetExcelShadowType.OuterRight);
shape.Effect.SetPresetGlow(ePresetExcelGlowType.Accent3_8Pt);

Shape

Read existing shapes

The Drawings collection can contain different type of objects (beside Shapes it might also contain Pictures, Charts and Form Controls).Here is how you can filter out the Shapes for a worksheet and cast them to the specialized ExcelShape type:

var shapes = worksheet.Drawings.Where(x => x.DrawingType == eDrawingType.Shape).Select(x => x.As.Shape);
foreach(var shape in shapes)
{
    var name = shape.Name;
    var shapeStyle = shape.Style;
    // etc
}

Remove a Shape

You can remove a shape from a worksheet via the Drawings.Remove method.

// remove by instance
var itemToRemove = worksheet.Drawings.Where(x => x.Name == "myShape").FirstOrDefault();
if(itemToRemove != null)
{
    worksheet.Drawings.Remove(itemToRemove);
}

// remove by index
worksheet.Drawings.Remove(0);

// remove by name
worksheet.Drawings.Remove("myShape");

See also

Sample 5.1 (C#) or Sample 5.1 (VB)

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally