GcExcel .NET allows you to embed drawing objects like shapes and pictures on cells of a worksheet. You can work with shape and picture by accessing the properties and methods of the IShape interface and the IShapes interface.
With GcExcel library, you can create different shape types such as Connector, Shape and Picture.
A connector is used when you need to connect or disconnect two general shapes. In GcExcel, you can use the BeginConnect method, EndConnect method, BeginDisconnect method and EndDisconnect method of the IConnectorFormat interface to attach and detach the ends of the connector to other shapes.
Refer to the following example code to connect general shapes using the connector format.
C# |
Copy Code |
---|---|
// To config the connector shape. IShape shapeBegin = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, 1, 1, 100, 100); IShape endBegin = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, 200, 200, 100, 100); IShape connectorShape = worksheet.Shapes.AddConnector(ConnectorType.Straight, 1, 1, 101, 101); connectorShape.Width = 10; // To detach the ends of the connector to other shapes. connectorShape.ConnectorFormat.BeginConnect(shapeBegin, 3); connectorShape.ConnectorFormat.EndConnect(endBegin, 0); |
A shape is a drawing object and a member of the Shapes collection. In GcExcel, the Shapes collection represents the collection of shapes in a specified worksheet. All the drawing objects including chart, comment, picture, slicer, general shape and shape group are defined as Shape.
A name can also be assigned to a shape, be it a chart, picture, connector or any autoshape, by using different methods provided in IShapes interface. By assigning a name to a shape, it be directly accessed and its properties can be modified rather than traversing through the list of all shapes.
Refer to the below example code to assign a name to an autoshape.
C# |
Copy Code |
---|---|
Workbook workbook = new Workbook(); // Fetch default worksheet IWorksheet worksheet = workbook.Worksheets[0]; //Create shape with custom name IShape shape = worksheet.Shapes.AddShape("Balloon", AutoShapeType.Balloon, 50, 50, 100, 200); //save to an excel file workbook.Save("BalloonShape.xlsx"); |
Refer to the below example code to assign a name to a chart.
C# |
Copy Code |
---|---|
//create a new workbook Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.Worksheets[0]; //set chart name IShape shape = worksheet.Shapes.AddChart("Area chart with custom name", ChartType.Area, 250, 20, 360, 230); worksheet.Range["A1:C13"].Value = new object[,] { { null, "Blue Series", "Orange Series" }, { "Jan", 0, 59.1883603948205 }, { "Feb", 44.6420211591501, 52.2280901938606 }, { "Mar", 45.2174930051225, 49.8093056416248 }, { "Apr", 62, 37.3065749226828 }, { "May", 53, 34.4312192530766 }, { "Jun", 31.8933622049831, 69.7834561753736 }, { "Jul", 41.7930895085093, 63.9418103906982 }, { "Aug", 73, 57.4049534494926 }, { "Sep", 49.8773891668518, 33 }, { "Oct", 50, 74 }, { "Nov", 54.7658428630216, 22.9587876597096 }, { "Dec", 32, 54 }, }; //Get chart by name IShape areaChart = worksheet.Shapes["Area chart with custom name"]; areaChart.Chart.SeriesCollection.Add(worksheet.Range["A1:C13"], RowCol.Columns); areaChart.Chart.ChartTitle.Text = "Area Chart"; //save to an excel file workbook.Save("ChartName.xlsx"); |
You can insert pictures on cells of a spreadsheet by using the AddPicture method of the IShapes interface. The IPictureFormat interface in GcExcel allows users to customize and format pictures while working in a spreadsheet.
Refer to the following example code when working with picture in GcExcel:
C# |
Copy Code |
---|---|
// Add a picture IShape picture = worksheet.Shapes.AddPicture(@"Images\flower.jpg", 480, 10, 100, 100); // Fill the inserted picture picture.Fill.Solid(); picture.Fill.Color.RGB = Color.AliceBlue; //Customize the inserted picture picture.PictureFormat.Crop.PictureWidth = 80; |
Refer to the below example code to assign a name to a picture.
C# |
Copy Code |
---|---|
Workbook workbook = new Workbook(); // Fetch default worksheet IWorksheet worksheet = workbook.Worksheets[0]; //add picture with custom name IShape shape = worksheet.Shapes.AddPicture("Custom Name to Image", "image.png", 10, 10, 250, 150); //save to an excel file workbook.Save("PictureName.xlsx"); |
Working with shapes and pictures in the GcExcel library involves the following tasks:
Customize Shape Format and Shape Text
Control Position of Overlapping Shapes