Add Oval shape to Excel Chart in C#

We have demonstrated how to insert textbox to Excel worksheet in C#. Starts from Spire.XLS v7.11.1, we have add a new method of chart.Shapes.AddOval(left,top,right,bottom); to enable developers to add oval shape to excel chart directly. Developers can also add the text contents to the oval shape and format the style for the oval. This article will describe clearly how to insert oval shape to Excel chart in C#.

Step 1: Create a workbook and get the first worksheet from the workbook.

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];

Step 2: Add a chart to the worksheet.

Chart chart = sheet.Charts.Add();

Step 3: Add oval shape to Excel chart.

var shape = chart.Shapes.AddOval(20, 60, 500,400);

Step 4: Add the text to the oval shape and set the text alignment on the shape.

shape.Text = "Oval Shape added by Spire.XLS";
shape.HAlignment = CommentHAlignType.Center;
shape.VAlignment = CommentVAlignType.Center;

Step 5: Format the color for the oval shape.

((XlsOvalShape)shape).Line.ForeColor = Color.Blue;
((XlsOvalShape)shape).Fill.ForeColor = Color.Green;

Step 6: Save the document to file.

workbook.SaveToFile("Result.xlsx",ExcelVersion.Version2010);

Effective screenshot of Oval shape added to the Excel chart:

Add Oval shape to Excel Chart in C#

Full codes:

static void Main(string[] args)
{
    Workbook workbook = new Workbook();

    Worksheet sheet = workbook.Worksheets[0];

    Chart chart = sheet.Charts.Add();
             
    var shape = chart.Shapes.AddOval(20, 60, 500,400);

    shape.Text = "Oval Shape added by Spire.XLS";

    shape.HAlignment = CommentHAlignType.Center;
    shape.VAlignment = CommentVAlignType.Center;

    ((XlsOvalShape)shape).Line.ForeColor = Color.Blue;
    ((XlsOvalShape)shape).Fill.ForeColor = Color.Green;
   
    workbook.SaveToFile("Result.xlsx",ExcelVersion.Version2010);
          
}