Delete Images in Excel in Java

This article demonstrates how to remove a specific image or all images from an Excel worksheet using Spire.XLS for Java.

Delete specific image

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class DeleteSpecificImage {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook workbook = new Workbook();
        
        //Load an Excel file
        workbook.loadFromFile("Input.xlsx");

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

        //Delete a specific image by its index
        sheet.getPictures().get(1).remove();

        //Save the document
        workbook.saveToFile("DeleteSpecificImage.xlsx", ExcelVersion.Version2013);
    }

Delete all images

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class DeleteAllImages {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook workbook = new Workbook();

        //Load an Excel file
        workbook.loadFromFile("Input.xlsx");

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

        //Loop through the images inside the worksheet
        for (int i = sheet.getPictures().getCount() - 1; i >= 0; i--) {
            
            //Delete an image by its index
            sheet.getPictures().get(i).remove();
        }

        //Save the document
        workbook.saveToFile("DeleteAllImages.xlsx", ExcelVersion.Version2013);
    }
}