-
Notifications
You must be signed in to change notification settings - Fork 284
Shapes
Mats Alm edited this page Dec 7, 2023
·
19 revisions
EPPlus supports adding 187 different types of shapes (defined by the eShapeStyle
enum) with multiple ways of formatting.
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);
//Drawing with a pattern fill
var shape = worksheet.Drawings.AddShape("PatternFill", eShapeStyle.SmileyFace);
shape.SetPosition(0, 5, 4, 5);
shape.SetSize(250, 250);
shape.Fill.Style = eFillStyle.PatternFill;
shape.Fill.PatternFill.PatternType = eFillPatternStyle.DiagBrick;
shape.Fill.PatternFill.BackgroundColor.SetPresetColor(ePresetColor.Yellow);
shape.Fill.PatternFill.ForegroundColor.SetSystemColor(eSystemColor.GrayText);
shape.Border.Width = 2;
shape.Border.Fill.Style = eFillStyle.SolidFill;
shape.Border.Fill.SolidFill.Color.SetHslColor(90, 50, 25);
shape.Font.Fill.Color = Color.Black;
shape.Font.Bold = true;
shape.Text = "Smiley With Pattern Fill";
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
}
As with any other Drawing
you can use the SendToBack
and BringToFront
methods to adjust how a Shape is located vertically compared with other Drawings
.
shape.BringToFront();
// or
shape.SendToBack();
You can remove a shape from a worksheet via the Drawings.Remove
method.
// remove by instance
var itemToRemove = worksheet.Drawings.FirstOrDefault(x => x.Name == "myShape");
if(itemToRemove != null)
{
worksheet.Drawings.Remove(itemToRemove);
}
// remove by index
worksheet.Drawings.Remove(0);
// remove by name
worksheet.Drawings.Remove("myShape");
EPPlus Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles