Insert and Extract Image in Excel in Java

2019-12-26 09:22:39 Written by  support iceblue
Rate this item
(0 votes)

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);
    }
}

Insert and Extract Image in Excel in Java

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"));
    }
}

Insert and Extract Image in Excel in Java

Additional Info

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