Spire.XLS supports a wealth of image processing features. Addition, rotation, extraction, deletion, etc. In this paper, I will show you how to insert and extract images into an Excel document using Spire.XLS for Java.
Tools used: Free Spire.XLS for Java (free version) https://www.e-iceblue.com/Introduce/free-xls-for-java.html
Getting and installing Jar files:
Method 1: Download the jar file bag through the homepage. After downloading, unzip the file and install the Spire.xls.jar file under the lib folder into your Java program. https://www.e-iceblue.com/Download/xls-for-java-free.html
Method 2: Introduced by installing maven warehouse.
https://www.e-iceblue.com/Tutorials/Licensing/How-to-install-Spire.PDF-for-Java-from-Maven-Repository.html
Insert image into excel
import com.spire.xls.ExcelPicture;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class InsertImage {
public static void main(String[] args){
//Import an Excel document
Workbook workbook = new Workbook();
workbook.loadFromFile("Input.xlsx");
//Get the first sheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Adds an image at the specified position on the sheet
ExcelPicture pic = sheet.getPictures().add(4, 1,"image.jpg ");
//Set the width and height of the image
pic.setWidth(500);
pic.setHeight(300);
//Save document
workbook.saveToFile("InsertImage.xlsx", ExcelVersion.Version2013);
}
}
Extract image
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 {
//Import an Excel document
Workbook workbook = new Workbook();
workbook.loadFromFile("InsertImage.xlsx");
//Get the first sheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Gets the first image of the sheet and saves it in the specified path
ExcelPicture pic = sheet.getPictures().get(0);
BufferedImage loImage = pic.getPicture();
ImageIO.write(loImage,"jpg",new File("output/ReadImage.jpg "));
}
}
Recommended Posts