Read Images from a xlsx file using Apache POI

This article is all about how to read and download images inside a file in xlsx format. We will use Apache POI using java.

1. Maven dependencies
  • commons-logging-1.2.jar
  •  commons-collections4-4.1.jar
  • openxml4j-1.0-betapoi-3.16.jar
  • poi-ooxml-3.16.jar
  • poi-ooxml-schemas-3.16.jar
  • xmlbeans-2.6.0.jar
  • servlet-api
2. Sharing java code


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class GetImageShapeFromExcel {

    @SuppressWarnings("resource")
    public static void main(String[] args) {
        try {
            String fileLoc = "/Users/Desktop/TestExcel.xlsx";
            String targetDir="/Users/Desktop/images/”;
            System.out.println("Reading fileLoc :" + fileLoc);
           
           
           
            InputStream ExcelFileToRead = new FileInputStream(fileLoc);
            XSSFWorkbook  wb = new XSSFWorkbook(ExcelFileToRead);
            XSSFSheet sheet = wb.getSheetAt(0);

            XSSFDrawing xssDrawing = sheet.getDrawingPatriarch();

            List<XSSFShape> shapes = xssDrawing.getShapes();
                       
            Iterator rows = sheet.rowIterator();
            XSSFRow row;
            XSSFCell cell;
       
           
           
            int shapeCount=0; //skiping first
            for (XSSFShape shape : shapes) {
               
               
               
                if (shape instanceof XSSFPicture) {
                   
                   
                    XSSFPicture hssfPicture = (XSSFPicture) shape;
                    PictureData data = hssfPicture.getPictureData();
                    byte [] picData = data.getData();
                    FileOutputStream fos = new FileOutputStream(new File(targetDir+”image_”+ shapeCount +”.jpg"));
                    fos.write(picData);
                    fos.close();
                   
                    //String filename = hssfPicture.getFileName();
                    int rowIndex = hssfPicture.getClientAnchor().getRow1();
                    int colIndex = hssfPicture.getClientAnchor().getCol1();
                    System.out.println("Picture  is located row: " + rowIndex+ ", col: " + colIndex);
                   
                }
               
                shapeCount++;
            }
           
       
           

        } catch (FileNotFoundException e) {
            System.out.println("FileNotFoundException : " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOException : " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Comments

Popular posts from this blog

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'

Upload csv file at Google Cloud Storage using java