News Category

Insert and remove shapes in Excel in Java

2021-04-09 07:26:06 Written by  support iceblue
Rate this item
(0 votes)

This article will demonstrate how to insert and remove shapes in Excel file using Spire.XLS for Java.

Add shapes to Excel worksheet:

import com.spire.xls.*;
import com.spire.xls.core.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class addShapestoExcel {
    public static void main(String[] args) throws Exception {

        String output = "output/AddShapesToExcelSheet.xlsx";

        //create a workbook.
        Workbook workbook = new Workbook();

        //get the first worksheet.
        Worksheet sheet = workbook.getWorksheets().get(0);

        //add a triangle shape.
        IPrstGeomShape triangle = sheet.getPrstGeomShapes().addPrstGeomShape(2, 2, 100, 100, PrstGeomShapeType.Triangle);
        //fill the triangle with solid color.
        triangle.getFill().setForeColor( Color.YELLOW);
        triangle.getFill().setFillType( ShapeFillType.SolidColor);

        //add a heart shape.
        IPrstGeomShape heart = sheet.getPrstGeomShapes().addPrstGeomShape(2, 5, 100, 100, PrstGeomShapeType.Heart);
        //fill the heart with gradient color.
        heart.getFill().setForeColor(Color.RED);
        heart.getFill().setFillType(ShapeFillType.Gradient);

        //add an arrow shape with default color.
        IPrstGeomShape arrow = sheet.getPrstGeomShapes().addPrstGeomShape(10, 2, 100, 100, PrstGeomShapeType.CurvedRightArrow);

        //add a cloud shape.
        IPrstGeomShape cloud = sheet.getPrstGeomShapes().addPrstGeomShape(10, 5, 100, 100, PrstGeomShapeType.Cloud);
        //fill the cloud with custom picture
        BufferedImage image = ImageIO.read(new File("SpireXls.png"));
        cloud.getFill().customPicture(image, "SpireXls.png");
        cloud.getFill().setFillType( ShapeFillType.Picture);

        //save to file.
        workbook.saveToFile(output, ExcelVersion.Version2013);
        }
}

Output:

Insert and remove shapes in Excel in Java

Remove a particular shape or all shapes from Excel worksheet:

import com.spire.xls.*;

public class removeShape {
    public static void main(String[] args) throws Exception {

                 //Load the sample file
                Workbook workbook = new Workbook();
                workbook.loadFromFile("output/AddShapesToExcelSheet.xlsx");

                //get the first worksheet.
                Worksheet sheet = workbook.getWorksheets().get(0);

                //delete the second shape in the worksheet
                sheet.getPrstGeomShapes().get(1).remove();

              /* //delete all shapes in the worksheet
                for (int i = sheet.getPrstGeomShapes().getCount()-1; i >= 0; i--)
                 {
                   sheet.getPrstGeomShapes().get(i).remove();
                 }*/

                //save to file.
                workbook.saveToFile("output/RemoveParticularShape.xlsx", ExcelVersion.Version2013);
            }
        }

Effective screenshot after remove the second shape from Excel worksheet:

Insert and remove shapes in Excel in Java

Additional Info

  • tutorial_title:
Last modified on Wednesday, 01 September 2021 02:35