Shapes Pictures Controls and Charts
Shapes, Pictures, Controls and Charts¶
Shapes, Pictures, Form Controls and Charts are all added via the Drawings collection of the ExcelWorksheet class.
EPPlus 5 and up supports several new styling features for Drawing objects like shadows, glows, blurs and 3D effects.
Shapes¶
Supports adding 187 different types of shapes with multiple ways of formatting.
Example¶
var shape = worksheet.Drawings.AddShape("myShape", eShapeStyle.Rect);
shape.SetPosition(1, 5, 6, 5); //Position Row, RowOffsetPixels, Column, ColumnOffsetPixels
shape.SetSize(400, 200); //Size in pixels
//Add some effects and set the text
shape.Effect.SetPresetShadow(ePresetExcelShadowType.OuterBottomRight);
shape.Effect.OuterShadow.Distance = 10;
shape.Effect.SetPresetSoftEdges(ePresetExcelSoftEdgesType.SoftEdge5Pt);
shape.Text = "This is a rectangular shape.";
¶
var shape = worksheet.Drawings.AddShape("myShape", eShapeStyle.Rect);
shape.SetPosition(1, 5, 6, 5); //Position Row, RowOffsetPixels, Column, ColumnOffsetPixels
shape.SetSize(400, 200); //Size in pixels
//Add some effects and set the text
shape.Effect.SetPresetShadow(ePresetExcelShadowType.OuterBottomRight);
shape.Effect.OuterShadow.Distance = 10;
shape.Effect.SetPresetSoftEdges(ePresetExcelSoftEdgesType.SoftEdge5Pt);
shape.Text = "This is a rectangular shape.";
Pictures¶
EPPlus supports adding pictures/images from a file, a stream or via an image object.
Example¶
Adding a picture/image to the workbook will work in a very similar way as adding a Shape
//Add an jpg image and apply some effects (EPPlus 6+ interface).
var pic = worksheet.Drawings.AddPicture("Landscape", new FileInfo("c:\\temp\\LandscapeView.jpg"));
pic.SetPosition(2, 0, 1, 0);
pic.Effect.SetPresetShadow(ePresetExcelShadowType.OuterBottomRight);
pic.Effect.OuterShadow.Distance = 10;
pic.Effect.SetPresetSoftEdges(ePresetExcelSoftEdgesType.SoftEdge5Pt);
Formats supported by EPPlus 5.x¶
- Jpeg
- Gif
- Png
- Tiff
- Bmp
- Emf (Windows)
- Wmf (Windows)
Formats supported by EPPlus 6+¶
- Jpeg
- Gif
- Png
- Tiff
- Bmp
- Emf (all platforms)
- Wmf (all platforms)
- ico
- svg
- webp
Charts¶
EPPlus supports all Excel 2019 chart types with modern chart styling. See this list of supported charts and our charts samples.
Example 1 - add a Pie chart¶
To add a chart use the AddChart method or even better its typed variant (in this case AddPieChart).
//Add the piechart
var pieChart = worksheet.Drawings.AddPieChart("crtExtensionsSize", ePieChartType.PieExploded3D);
//Set top left corner to row 1 column 2
pieChart.SetPosition(1, 0, 2, 0);
pieChart.SetSize(400, 400);
pieChart.Series.Add(ExcelRange.GetAddress(3, 2, row-1, 2), ExcelRange.GetAddress(3, 1, row-1, 1));
pieChart.Title.Text = "Extension Size";
//Set datalabels and remove the legend
pieChart.DataLabel.ShowCategory = true;
pieChart.DataLabel.ShowPercent = true;
pieChart.DataLabel.ShowLeaderLines = true;
pieChart.Legend.Remove();
Example 2 - add a Sunburst chart¶
From EPPlus 5.2, all chart types are supported, even newer types like Sunburst Charts and Region Maps.
Here's a piece of code from the sample project showing how to add a Sunburst Chart:
var ws = package.Workbook.Worksheets.Add("Sunburst & Treemap Chart");
var range = await LoadSalesFromDatabase(connectionString, ws);
var sunburstChart = ws.Drawings.AddSunburstChart("SunburstChart1");
var sbSerie = sunburstChart.Series.Add(ws.Cells[2, 4, range.Rows, 4], ws.Cells[2, 1, range.Rows, 3]);
sbSerie.HeaderAddress = ws.Cells["D1"];
sunburstChart.SetPosition(1, 0, 6, 0);
sunburstChart.SetSize(800, 800);
sunburstChart.Title.Text = "Sales";
sunburstChart.Legend.Add();
sunburstChart.Legend.Position = eLegendPosition.Bottom;
sbSerie.DataLabel.Add(true, true);
sunburstChart.StyleManager.SetChartStyle(ePresetChartStyle.SunburstChartStyle3);
Form Controls¶
From EPPlus 5.5 adding, removing and modifying form controls is supported.
Supported form controls are:
* Buttons
* Drop-Downs
* List Boxes
* Check Boxes
* Radio Buttons
* Spin Buttons
* Scroll Bars
* Labels
* Group Boxes
Form controls can be linked to a cell or connected to a macro. See Form-Controls for more details
See also¶
For more details have a look at sample 5 in the sample project Sample 5-C# or Sample 5-VB.