Java: Insert Images into Excel
Images are visual representations of information. Adding images to our documents can help us express our thoughts in a simple and beautiful way. In this article, we will introduce how to insert images into Excel in Java using Spire.XLS for Java library.
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>14.4.1</version> </dependency> </dependencies>
Insert Image from Disk into Excel in Java
The following are the steps to insert an image from disk into Excel:
- Initialize a Workbook instance
- Get the desired worksheet using Workbook.getWorksheets().get(sheetIndex) method.
- Insert an image into the worksheet using Worksheet.getPictures().add() method.
- Save the result file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class InsertImageFromDisk { public static void main(String[] args){ //Initialize a Workbook instance Workbook workbook = new Workbook(); //Get the first sheet Worksheet sheet = workbook.getWorksheets().get(0); //Insert an image into the worksheet sheet.getPictures().add(1, 1,"E:\\work\\sample.jpg"); //Save the result file workbook.saveToFile("InsertImageFromDisk.xlsx", ExcelVersion.Version2016); } }
Insert Web Image from a URL into Excel in Java
The following are the steps to insert a web image from a URL into an Excel worksheet:
- Initialize a Workbook instance.
- Get the desired worksheet using Workbook.getWorksheets().get(sheetIndex) method.
- Initialize a URL instance to get the image from the specified URL. Within the constructor of URL class, pass the image’s URL as a parameter.
- Read the image into a BufferedImage object using ImageIO.read() method.
- Insert the image into the worksheet using Worksheet.getPictures().add() method.
- Save the result file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; public class InsertWebImage { public static void main(String[] args) throws IOException { //Initialize a Workbook instance Workbook workbook = new Workbook(); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Initialize a URL instance to get the image from the specified URL URL url = new URL("https://cdn.e-iceblue.com/downloads/demo/Logo.png"); //Read the image into a BufferedImage object BufferedImage bufferedImage = ImageIO.read(url); //Insert the image into the worksheet sheet.getPictures().add(3, 2, bufferedImage ); //Save the result file workbook.saveToFile("InsertWebImage.xlsx", ExcelVersion.Version2016); } }
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.
Java replace the text with image in Excel worksheet
This article will show you how to replace the searched text with image in Excel worksheet by using Spire.XLS in Java applications.
Sample Excel:
import com.spire.xls.*; import java.io.IOException; public class replaceTextwithImage { public static void main(String[] args) throws IOException { //Load the sample Excel document Workbook workbook = new Workbook(); workbook.loadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet worksheet = workbook.getWorksheets().get(0); //Find the text string {{Image}} CellRange[] ranges = worksheet.findAllString("{{Image}}", false, false); for (CellRange range : ranges) { //set the text as null range.setText(""); //get the row and column of the searched range int row = range.getRow(); int column = range.getColumn(); //Add the image to the searched range worksheet.getPictures().add(row, column, "logo.jpg", ImageFormatType.Jpeg); //Save the document to file workbook.saveToFile("replaceTextwithImage.xlsx", ExcelVersion.Version2013); } } }
Output:
Insert and remove shapes in Excel in Java
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:
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:
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); } }
Insert and Extract Image in Excel in Java
This article demonstrates how to insert and extract image in Excel file using Spire.XLS for Java.
Insert image
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){ //Create a Workbook instance Workbook workbook = new Workbook(); //Load an Excel file workbook.loadFromFile("Input.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Add an image to the specific cell ExcelPicture pic = sheet.getPictures().add(4, 1,"image.jpg"); //Set image width and height pic.setWidth(500); pic.setHeight(300); //Save the result file 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 { //Create a Workbook instance Workbook workbook = new Workbook(); //Load an Excel file workbook.loadFromFile("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")); } }