Delete shapes in an Excel Worksheet in C#

Spire.XLS supports to delete a specific shape as well as all shapes in an Excel worksheet. This article demonstrates how to use Spire.XLS to implement this function.

The example file we used for demonstration:

Delete shapes in an Excel Worksheet in C#

Detail steps:

Step 1: Initialize an object of Workbook class and load the Excel file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xlsx");

Step 2: Get the first worksheet.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Delete the first shape in the worksheet.

sheet.PrstGeomShapes[0].Remove();

To delete all shapes from the worksheet:

for (int i = sheet.PrstGeomShapes.Count-1; i >= 0; i--)
{
    sheet.PrstGeomShapes[i].Remove();
}

Step 4: Save the file.

workbook.SaveToFile("DeleteShape.xlsx", ExcelVersion.Version2013);

Screenshot:

Delete shapes in an Excel Worksheet in C#

Full code:

using Spire.Xls;
namespace DeleteShape
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an object of Workbook class
            Workbook workbook = new Workbook();
            //Load the Excel file
            workbook.LoadFromFile("Input.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Delete the first shape in the worksheet
            sheet.PrstGeomShapes[0].Remove();

            //Delete all shapes in the worksheet
            //for (int i = sheet.PrstGeomShapes.Count-1; i >= 0; i--)
            //{
            //    sheet.PrstGeomShapes[i].Remove();
            //}

            workbook.SaveToFile("DeleteShape.xlsx", ExcelVersion.Version2013);
        }
    }
}