C#/VB.NET: Group or Ungroup Shapes in Excel

2022-12-09 01:42:11 Written by  support iceblue
Rate this item
(3 votes)

Sometimes, you may need to perform specific operations on multiple shapes in Excel, such as adding styles, resizing, or moving them around. Grouping shapes can help you accomplish this task more easily and efficiently. Once shapes are grouped, they are treated as a single entity and can be manipulated at the same time. In this article, you will learn how to programmatically group or ungroup shapes in Excel in C# and VB.NET using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Group Shapes in Excel using C# and VB.NET

To group certain shapes in an Excel worksheet, you need to use the Worksheet.GroupShapeCollection property to return a GroupShapeCollection object, and then call the GroupShapeCollection.Group() method. The following are the detailed steps:

  • Initialize an instance of Workbook class.
  • Get the first worksheet by its index through Workbook.Worksheets[int] property.
  • Add several shapes to specific rows and columns in the worksheet using Worksheet.PrstGeomShapes.AddPrstGeomShape(int, int, int, int, PrstGeomShapeType) method.
  • Get the group shape collection of the worksheet through Worksheet.GroupShapeCollection property.
  • Group the shapes using GroupShapeCollection.Group(IShape[]) method.
  • Save the result document using Workbook.SaveToFile(string) method.
  • C#
  • VB.NET
using Spire.Xls;
using Spire.Xls.Core;
using Spire.Xls.Core.MergeSpreadsheet.Collections;
using System.Drawing;

namespace GroupShapes
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            //Get the first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            //Add four shapes
            IPrstGeomShape shape1 = worksheet.PrstGeomShapes.AddPrstGeomShape(3, 3, 65, 50, PrstGeomShapeType.RoundRect);
            shape1.Fill.FillType = ShapeFillType.SolidColor;
            shape1.Fill.ForeColor = Color.Yellow;
            shape1.Line.Weight = 0.1;
            IPrstGeomShape shape2 = worksheet.PrstGeomShapes.AddPrstGeomShape(7, 3, 65, 50, PrstGeomShapeType.Ribbon);
            shape2.Fill.FillType = ShapeFillType.SolidColor;
            shape2.Fill.ForeColor = Color.Purple;
            shape2.Line.Weight = 0.1;
            IPrstGeomShape shape3 = worksheet.PrstGeomShapes.AddPrstGeomShape(3, 5, 65, 50, PrstGeomShapeType.Cloud);
            shape3.Fill.FillType = ShapeFillType.SolidColor;
            shape3.Fill.ForeColor = Color.LightGreen;
            shape3.Line.Weight = 0.1;
            IPrstGeomShape shape4 = worksheet.PrstGeomShapes.AddPrstGeomShape(7, 5, 65, 50, PrstGeomShapeType.Ellipse);
            shape4.Fill.FillType = ShapeFillType.SolidColor;
            shape4.Fill.ForeColor = Color.LightSkyBlue;
            shape4.Line.Weight = 0.1;

            //Group the shapes
            GroupShapeCollection groupShapeCollection = worksheet.GroupShapeCollection;
            groupShapeCollection.Group(new IShape[] { shape1, shape2, shape3, shape4});


            //Save the result file
            workbook.SaveToFile("GroupShapes.xlsx", ExcelVersion.Version2013);
        }
    }
}

C#/VB.NET: Group or Ungroup Shapes in Excel

Ungroup Shapes in Excel using C# and VB.NET

To ungroup the grouped shapes in an Excel worksheet, you can use the GroupShapeCollection. UnGroupAll() method. After the shapes are ungrouped, you can manipulate them individually. The following are the detailed steps:

  • Initialize an instance of Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet by its index through Workbook.Worksheets[int] property.
  • Get the group shape collection of the worksheet through Worksheet.GroupShapeCollection property.
  • Ungroup all the grouped shapes using GroupShapeCollection.UnGroupAll() method.
  • Save the result document using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
using Spire.Xls.Core.MergeSpreadsheet.Collections;

namespace UngroupShapes
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("GroupShapes.xlsx");

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

            //Ungroup the grouped shapes in the worksheet
            GroupShapeCollection groupShapeCollection = worksheet.GroupShapeCollection;
            groupShapeCollection.UnGroupAll();

            //Save the result file
            workbook.SaveToFile("UnGroupShapes.xlsx", ExcelVersion.Version2013);
        }
    }
}

C#/VB.NET: Group or Ungroup Shapes in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Friday, 09 December 2022 01:45