Thursday, 24 September 2020

Insert, Extract and Delete Images in Excel using Java

Although Excel documents are primarily used as a calculation program, in some situations you may want to store pictures along with data and associate an image with a particular piece of information. In this tutorial, I will show you how to insert an image to Excel, extract and delete images in Excel using Java programmatically without Microsoft Office installed in your system.

I.       How to insert an image to Excel

II.     How to extract images in Excel

III.   How to delete images in Excel

   A.  How to delete a specific image in Excel

   B.  How to delete all images in Excel

The Java API I used in this tutorial is Free Spire.XLS for Java, which enables developers to create, manage, manipulate, convert and print Excel worksheets without using Microsoft Office or Microsoft Excel.

Add Spire.Xls.jar to your project as a dependency

Method 1: Download the package Free Spire.XLS for Java from the link, unzip it and you’ll find Spire.Xls.jar in the “lib” folder. Finally just manually add it to your project as a dependency.

Method 2(Recommend): Install Spire.XLS for Java from Maven repository. Please create a Maven project, and specify e-iceblue Maven Repository configuration and define Spire.PDF for Java API dependency in your pom.xml as follows.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>2.2.0</version>
    </dependency>
</dependencies>

Finally, just click “Import Changes” to import the Spire.Pdf.jar. The following screenshot is what

it looks like finally.


How to insert an image to Excel?

Free Spire.XLS for Java offers the ExcelPicture method to insert an image to Excel file.

import com.spire.xls.ExcelPicture;

import com.spire.xls.ExcelVersion;

import com.spire.xls.Workbook;

import com.spire.xls.Worksheet;
public class AddImage {
   
public static void main(String[] args) {
       
//Create a Workbook instance
       
Workbook workbook = new Workbook();
       
//Load an Excel file
       
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");
       
//Get the first worksheet
       
Worksheet sheet = workbook.getWorksheets().get(0);
       
//Add an image to the specific cell
       
ExcelPicture pic = sheet.getPictures().add(4, 2,"C:\\Users\\Test1\\Desktop\\Image.png");
       
//Set width and height of the image
       
pic.setWidth(400);
        pic.setHeight(
200);
       
//Save the resulting file
       
workbook.saveToFile("output/InsertImage.xlsx", ExcelVersion.Version2013);
    }
}

Output


How to extract images in Excel?

The first thing to extract images in Excel is load an Excel file and get the image in it, and then save

the image to disk.

import com.spire.xls.ExcelPicture;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ReadImage {
   
public static void main(String[] args) throws IOException {
       
//Create a Workbook instance
       
Workbook workbook = new Workbook();
        
//Load an Excel file
       
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\InsertImage.xlsx");
       
//Get the first worksheet
       
Worksheet sheet = workbook.getWorksheets().get(0);         //Get the first image in the worksheet
       
ExcelPicture pic = sheet.getPictures().get(0);
        BufferedImage loImage = pic.getPicture();
       
//Save to disk
       
ImageIO.write(loImage,"jpg",new File("output/ReadImage.jpg"));
    }
}

Output


Delete Images in Excel

Free Spire.XLS for Java not only supports deleting a specific image in Excel file by its index, but also

can delete all images using the “for” loop method.

l  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("C:\\Users\\Test1\\Desktop\\InsertImage.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("output/DeleteSpecificImage.xlsx", ExcelVersion.Version2013);
    }
}

l  Delete All images

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class DeleteAllImage {
   
public static void main(String[] args) {
       
//Create a Workbook object
       
Workbook workbook = new Workbook();
       
//Load an Excel file
       
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\InsertImage.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 all images
            
sheet.getPictures().get(i).remove();
        }
       
//Save the document
       
workbook.saveToFile("output/DeleteAllImages.xlsx", ExcelVersion.Version2013);
    }
}

Conclusion

Through this tutorial, I believe you can better manipulate images in Excel file using Java programmatically.

In addition to those functions, Free Spire.XLS for Java has the ability to support amounts of other features.

Click the link for knowing more information.

No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...