Java replace the text with image in Excel worksheet

2021-04-26 05:33:32 Written by  support iceblue
Rate this item
(0 votes)

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:

Java replace the text with image in Excel worksheet

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:

Java replace the text with image in Excel worksheet

Additional Info

  • tutorial_title:
Last modified on Wednesday, 01 September 2021 02:35