Java read excel with Apache Poi Java API

Java leer excel con Apache Poi Java API

In any application or development is often necessary to process Excel files or other spreadsheets, in this case, we will focus on Microsoft’s OLE 2 documents, and manipulate them in this case using Apache POI – the Java API for Microsoft Documents,that provides access to different file types Microsoft that use this structure as Excel, Word or Powerpoint, there are other projects in this API to Visio and Publisher for example all these the more developed Excel Workbooks.

Performed the introduction, we will introduce the elements of this library that we will use to read and create an Excel spreadsheet.

  • HSSFWorkbook: High level representation of a workbook. This is the first object will construct whether they are reading or writing a workbook.
  • HSSFSheet: high level representation of a worksheet, we can choose the sheet using the HSSFWorkBook.
  • HSSFRow: representation of a row of a spreadsheet, only rows that have cells should be added to a Sheet.
  • HSSFCell: representation of a cell in a row of a spreadsheet, we use to manage the contents of the cell.

Adding de library Apache Poi Java API

After all we need to download the Apache Poi Java API, then we add it to our project, I’m going to explain how to do it in the IDE I’m using for this example: Netbeans, in other IDEs this will be similar.

In our project we seek Libraries folder we stand up and select Add Library, not much to explain so I’ll show you in pictures:

 

Java read excel with Apache Poi Java API

First we are going to read a basic Excel file and show it in the console, in my example I use this Excel file with countries, currencies and languages:


 
This is your content:
Excel de países que utilizamos

Excel de países que utilizamos


 
Following the Xules Code where you have all the explanations of what is done:

 
package org.xulescode.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Cell; 

/** 
 * Utility class, where we will create methods for training read and write excel files,
 * with <a href="https://poi.apache.org/">Apache POI</a>, we use 
 * <a href="https://poi.apache.org/spreadsheet/">POI-HSSF and POI-XSSF - Java API To Access Microsoft</a>
 * HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file.
 * 
 * Clase de utilidades, donde crearemos métodos
 * para el aprendizaje de la lectura y escritura de ficheros excel con 
 * <a href="https://poi.apache.org/">Apache POI</a>, usaremos
 * <a href="https://poi.apache.org/spreadsheet/">POI-HSSF and POI-XSSF - Java API To Access Microsoft</a>
 * HSSF es el proyecto POI de implementación total en Java para ficheros Excel '97(-2007).
 *
 * @author Xules You can follow me on my website http://www.codigoxules.org/en
 * Puedes seguirme en mi web http://www.codigoxules.org).
 */
public class JavaPoiUtils {
    /**
     * Explanation of the method by which we read the excel file we pass as
     * parameter if exists, in this example we print the content in the console.
     * Explicación del método con el que leemos el fichero excel que pasamos como
     * parámetro si existe, en este ejemplo mostramos el contenido por la consola.
     * <h3>Example (Ejemplo)</h3>
     * <pre>
     * JavaPoiUtils javaPoiUtils = new JavaPoiUtils();
     * javaPoiUtils.readExcelFile(new File("/home/xules/codigoxules/apachepoi/PaisesIdiomasMonedas.xls"));    
     * </pre>
     *
     * @param excelFile <code>String</code> 
     *      excel File we are going to read. 
     *      Fichero excel que vamos a leer. 
     */
    public void readExcelFile(File excelFile){
        InputStream excelStream = null;
        try {
            excelStream = new FileInputStream(excelFile);
            // High level representation of a workbook.
            // Representación del más alto nivel de la hoja excel.
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(excelStream);
            // We chose the sheet is passed as parameter. 
            // Elegimos la hoja que se pasa por parámetro.
            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
            // An object that allows us to read a row of the excel sheet, and extract from it the cell contents.
            // Objeto que nos permite leer un fila de la hoja excel, y de aquí extraer el contenido de las celdas.
            HSSFRow hssfRow;
            // Initialize the object to read the value of the cell 
            // Inicializo el objeto que leerá el valor de la celda
            HSSFCell cell;                        
            // I get the number of rows occupied on the sheet
            // Obtengo el número de filas ocupadas en la hoja
            int rows = hssfSheet.getLastRowNum();
            // I get the number of columns occupied on the sheet
            // Obtengo el número de columnas ocupadas en la hoja
            int cols = 0;            
            // A string used to store the reading cell
            // Cadena que usamos para almacenar la lectura de la celda
            String cellValue;  
            // For this example we'll loop through the rows getting the data we want
            // Para este ejemplo vamos a recorrer las filas obteniendo los datos que queremos            
            for (int r = 0; r < rows; r++) {
                hssfRow = hssfSheet.getRow(r);
                if (hssfRow == null){
                    break;
                }else{
                    System.out.print("Row: " + r + " -> ");
                    for (int c = 0; c < (cols = hssfRow.getLastCellNum()); c++) {
                        /* 
                            We have those cell types (tenemos estos tipos de celda): 
                                CELL_TYPE_BLANK, CELL_TYPE_NUMERIC, CELL_TYPE_BLANK, CELL_TYPE_FORMULA, CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR
                        */
                        cellValue = hssfRow.getCell(c) == null?"":
                                (hssfRow.getCell(c).getCellType() == Cell.CELL_TYPE_STRING)?hssfRow.getCell(c).getStringCellValue():
                                (hssfRow.getCell(c).getCellType() == Cell.CELL_TYPE_NUMERIC)?"" + hssfRow.getCell(c).getNumericCellValue():
                                (hssfRow.getCell(c).getCellType() == Cell.CELL_TYPE_BOOLEAN)?"" + hssfRow.getCell(c).getBooleanCellValue():
                                (hssfRow.getCell(c).getCellType() == Cell.CELL_TYPE_BLANK)?"BLANK":
                                (hssfRow.getCell(c).getCellType() == Cell.CELL_TYPE_FORMULA)?"FORMULA":
                                (hssfRow.getCell(c).getCellType() == Cell.CELL_TYPE_ERROR)?"ERROR":"";                       
                        System.out.print("[Column " + c + ": " + cellValue + "] ");
                    }
                    System.out.println();
                }
            }            
        } catch (FileNotFoundException fileNotFoundException) {
            System.out.println("The file not exists (No se encontró el fichero): " + fileNotFoundException);
        } catch (IOException ex) {
            System.out.println("Error in file procesing (Error al procesar el fichero): " + ex);
        } finally {
            try {
                excelStream.close();
            } catch (IOException ex) {
                System.out.println("Error in file processing after close it (Error al procesar el fichero después de cerrarlo): " + ex);
            }
        }
    }
    /**     
     * Main method for the tests for the methods of the class <strong>Java
     * read excel</strong> and <strong>Java create excel</strong> 
     * with <a href="https://poi.apache.org/">Apache POI</a>. 
     * <br />
     * Método main para las pruebas para los método de la clase,
     * pruebas de <strong>Java leer excel</strong> y  <strong>Java crear excel</strong>
     * con <a href="https://poi.apache.org/">Apache POI</a>.     
     * @param args 
     */
    public static void main(String[] args){
        JavaPoiUtils javaPoiUtils = new JavaPoiUtils();
        javaPoiUtils.readExcelFile(new File("/home/xules/codigoxules/apachepoi/PaisesIdiomasMonedas.xls"));        
    }    
}

Method main update that we use to check the result:

Java read Apache Poi Result - 01 - First Example

Java read Apache Poi Result – 01 – First Example


 

Java read excel returning an array with Apache Poi Java API improving reading

In this case we will create a new method to improve reading Excel spreadsheet making it a more efficient way, this is the structure that we will use:

    for (Sheet sheet : wb ) {
        for (Row row : sheet) {
            for (Cell cell : row) {
                // Do something here
            }
        }
    }

 
These iterators are available by calling workbook.sheetIterator(), sheet.rowIterator(), and row.cellIterator(), or implicitly using a for-each loop.

 
In the new method we use is prepared refund structure and data in an ArrayList, then in the main method we will check to verify that the array has been read correctly the Excel file:

    /**
     * Explanation of the method by which we read the excel file we pass as
     * parameter if exists, we return the excel file values in an ArrayList<>.
     * Explicación del método con el que leemos el fichero excel que pasamos como
     * parámetro si existe, devolvemos los valores de la hoja excel en un ArrayList<>.
     * <h3>Example (Ejemplo)</h3>
     * <pre>
     * JavaPoiUtils javaPoiUtils = new JavaPoiUtils();
     * javaPoiUtils.readExcelFile(new File("/home/xules/codigoxules/apachepoi/PaisesIdiomasMonedas.xls"));    
     * </pre>
     *
     * @param excelFile <code>String</code> 
     *      excel File we are going to read. 
     *      Fichero excel que vamos a leer.  
     * @return <code>ArrayList<String[]></code> we return the excel file values in an ArrayList<> (devolvemos los valores de la hoja excel en un ArrayList<>).
     */
    public ArrayList<String[]> readExcelFileToArray(File excelFile){    
        ArrayList<String[]> arrayDatos = new ArrayList<>();
        InputStream excelStream = null;
        try {
            excelStream = new FileInputStream(excelFile);
            // High level representation of a workbook.
            // Representación del más alto nivel de la hoja excel.
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(excelStream);
            // We chose the sheet is passed as parameter. 
            // Elegimos la hoja que se pasa por parámetro.
            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);    
            // An object that allows us to read a row of the excel sheet, and extract from it the cell contents.
            // Objeto que nos permite leer un fila de la hoja excel, y de aquí extraer el contenido de las celdas.
            HSSFRow hssfRow = hssfSheet.getRow(hssfSheet.getTopRow());
            String [] datos = new String[hssfRow.getLastCellNum()];            
            // For this example we'll loop through the rows getting the data we want
            // Para este ejemplo vamos a recorrer las filas obteniendo los datos que queremos            
            for (Row row: hssfSheet) {                    
                for (Cell cell : row) {
                    /* 
                        We have those cell types (tenemos estos tipos de celda): 
                            CELL_TYPE_BLANK, CELL_TYPE_NUMERIC, CELL_TYPE_BLANK, CELL_TYPE_FORMULA, CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR
                    */
                    datos[cell.getColumnIndex()] =  
                            (cell.getCellType() == Cell.CELL_TYPE_STRING)?cell.getStringCellValue():
                            (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)?"" + cell.getNumericCellValue():
                            (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN)?"" + cell.getBooleanCellValue():
                            (cell.getCellType() == Cell.CELL_TYPE_BLANK)?"BLANK":
                            (cell.getCellType() == Cell.CELL_TYPE_FORMULA)?"FORMULA":
                            (cell.getCellType() == Cell.CELL_TYPE_ERROR)?"ERROR":"";                                                                   
                }
                arrayDatos.add(datos); 
                datos = new String[hssfRow.getLastCellNum()];  
            }            
        } catch (FileNotFoundException fileNotFoundException) {
            System.out.println("The file not exists (No se encontró el fichero): " + fileNotFoundException);
        } catch (IOException ex) {
            System.out.println("Error in file procesing (Error al procesar el fichero): " + ex);
        } finally {
            try {
                excelStream.close();
            } catch (IOException ex) {
                System.out.println("Error in file processing after close it (Error al procesar el fichero después de cerrarlo): " + ex);
            }
        }
        return arrayDatos;
    }

 
Method main update that we use to check the result. :

    /**     
     * Main method for the tests for the methods of the class <strong>Java
     * read excel</strong> and <strong>Java create excel</strong> 
     * with <a href="https://poi.apache.org/">Apache POI</a>. 
     * <br />
     * Método main para las pruebas para los método de la clase,
     * pruebas de <strong>Java leer excel</strong> y  <strong>Java crear excel</strong>
     * con <a href="https://poi.apache.org/">Apache POI</a>.     
     * @param args 
     */
    public static void main(String[] args){
        JavaPoiUtils javaPoiUtils = new JavaPoiUtils();  
        ArrayList<String[]> arrayDatosExcel = javaPoiUtils.readExcelFileToArray(new File("/home/xules/codigoxules/apachepoi/PaisesIdiomasMonedas.xls")); 
        int r = 0;
        for (String[] next : arrayDatosExcel) {
            System.out.print("Array Row: " + r++ + " -> ");
            for (int c = 0; c < next.length; c++) {
                System.out.print("[Column " + c + ": " + next + "] ");
            }
            System.out.println();
        }
    }   

 
This is the final result that will be showed in the screen.

Java read excel Apache Poi Result - 01 - Second Example ArrayList

Java read excel Apache Poi Result – 01 – Second Example ArrayList


 

Documentation Java read excel with Apache Poi

I hope it has been useful for youXules

4 responses on “Java read excel with Apache Poi Java API

  1. Eduardo

    En la parte de System.out.print(“[Column ” + c + “: ” + next + “] “); te falto agregar el indice del arreglo next para que muestre el valor y no el objeto

    System.out.print(“[Column ” + c + “: ” + next[i] + “] “);

    1. Julio Yáñez Novo Post author

      Hola Wiliam.
      Si es posible con algunas limitaciones:

      https://poi.apache.org/spreadsheet/limitations.html

      • HSSF has some limited support for creating a handful of very simple Chart types, but largely this isn’t supported. HSSF (largely) doesn’t support changing Charts. You can however create a chart in Excel using Named ranges, modify the chart data values using HSSF and write a new spreadsheet out. This is possible because POI attempts to keep existing records intact as far as possible.
      • XSSF has only limited chart support including making some simple changes and adding at least some line and scatter charts, see the examples LineChart and ScatterChart.

      En este link : XSSF-only Examples – LineChart tienes un ejemplo de código para la creación de un gráfico lineal.

      Un saludo.
      Espero que te sirva de ayuda.

  2. fernando

    hola amigo una consulta utilize el codigo del ejemplo pero en mi aarchivo exel hay una columna con campo tipo fecha y me sale numeros si me puedea ayudar que cambio tocaria hacer al ejemplo en ese caso. garcias..

Leave a Reply

Your email address will not be published. Required fields are marked *