Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Shapes, Pictures and Charts

Jan Källman edited this page Sep 17, 2017 · 2 revisions

Shapes, Pictures and Charts are all added via the Drawings collection of the ExcelWorksheet class. For example

            //Add the textbox
            var shape = ws.Drawings.AddShape("myShape", eShapeStyle.Rect);
            shape.SetPosition(1, 5, 6, 5);       //Position Row, RowOffsetPixels, Column, ColumnOffsetPixels
            shape.SetSize(400, 200);             //Size in pixels
            shape.Text = "This is a rectangular shape.";

Adding an image to the workbook will work very similar way.

            var img = Image.FromFile("image.jpg");
            var pic = ws.Drawings.AddPicture(myPicture, img);
            shape.SetPosition(3, 0, 3, 0)       //Position Row, RowOffsetPixels, Column, ColumnOffsetPixels

To add a chart use the AddChart method...

            //Add the piechart
            var pieChart = (ExcelPieChart)ws.Drawings.AddChart("crtExtensionsSize", eChartType.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();

Note that the AddChart method will return a different chart class depending on what chart type you specify in parameter two. The base class is ExcelChart but the following charts will be returned.

  • ExcelBarChart
  • ExcelBubbleChart
  • ExcelChart
  • ExcelDoughnutChart
  • ExcelLineChart
  • ExcelOfPieChart
  • ExcelPieChart
  • ExcelRadarChart
  • ExcelScatterChart
  • ExcelSurfaceChart

For more details have a look at Sample 6 and Sample 9