When it comes to managing images in Excel, the ability to replace or extract them efficiently is important. If there are outdated or incorrect images in Excel, replacing them ensures that your data remains accurate and up-to-date. While extracting images from Excel files can be useful when you need to reuse them in other documents or presentations. In this article, you will learn how to replace or extract images in Excel in Java using Spire.XLS for Java.
Install Spire.XLS for Java
First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls</artifactId> <version>15.1.3</version> </dependency> </dependencies>
Replace Images in Excel in Java
To replace a picture in Excel, you can load a new picture and then pass it as a parameter to the ExcelPicture.setPicture() method. The following are the detailed steps to replace an Excel image with another one.
- Create a Workbook instance.
- Load an Excel file using Workbook.loadFromFile() method.
- Get the first worksheet using Workbook.getWorksheets().get() method.
- Get the first picture in the worksheet using Worksheet.getPictures().get() method.
- Load an image, and then pass it as a parameter to the ExcelPicture.setPicture() method to replace the original picture with the new one.
- Save the result file using Workbook.saveToFile() method.
- Java
import com.spire.xls.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; public class replaceImages { public static void main(String[] args) throws IOException { // Create a Workbook instance Workbook workbook = new Workbook(); // Load an Excel file workbook.loadFromFile("ExcelImg.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Get the first image in the worksheet ExcelPicture pic = sheet.getPictures().get(0); // Load an image BufferedImage bufferedImage = ImageIO.read(new File("logo.png")); // Replace the first image with the loaded one pic.setPicture(bufferedImage); // Save the result file workbook.saveToFile("ReplaceImage.xlsx", ExcelVersion.Version2016); } }
Extract Images from Excel in Java
With Spire.XLS for Java, you can also extract all images in an Excel worksheet at once. The following are the detailed steps.
- Create a Workbook instance.
- Load an Excel file using Workbook.loadFromFile() method.
- Get the first worksheet using Workbook.getWorksheets().get() method.
- Get all the images in the worksheet using Worksheet.getPictures() method.
- Loop through to get each image, and then save them to a specified file path.
- Java
import com.spire.xls.*; import com.spire.xls.collections.PicturesCollection; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; public class extractImages { public static void main(String[] args) throws IOException { // Create a Workbook instance Workbook workbook = new Workbook(); // Load an Excel file workbook.loadFromFile("Test.xlsx"); // Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); // Get all the pictures in the worksheet PicturesCollection pic = sheet.getPictures(); // Iterate through each image for (int i = pic.getCount() - 1; i >= 0; i--) { // Get a specified image ExcelPicture excelPic = pic.get(i); BufferedImage loImage = excelPic.getPicture(); // Save the image to a specified file path File file = new File(String.format("ExtractImages\\Image-%d.png", i)); ImageIO.write(loImage, "PNG", file); } } }
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.