Read and write Excel file using Apache POI

By xngo on February 21, 2019

Overview

Apache POI project provides Java API for read and write Microsoft documents: XLS, DOC, PPT, XLSX, DOCX and PPTX. The code below shows how to write and read Excel file(i.e *.xls or *.xlsx).

Installation

  • Download the binary library from Apache POI download page.
  • Extract it.
  • Put poi-4.0.1.jar and poi-ooxml-4.0.1.jar in a folder, e.g. lib.
  • Add both JARs in your classpath.
    1. For Eclipse, right-click on your project.
    2. Select Properties.
    3. Select Java Build Path.
    4. Click on Libraries tab.
    5. Click on Add JARs... button.

The code

package net.openwritings.xngo.java.poi;
 
/**
 * Example showing how to write and read Excel file(i.e *.xls or *.xlsx).
 * JAR files needed:
 *    poi-*.jar
 *    poi-ooxml-*.jar
 * If you need to handle Excel 2007 OOXML (.xlsx) file format, then use XSSF* classes.
 * If you need to handle Excel 97-2003(.xls) file format, then use HSSF* classes.
 * @author Xuan Ngo
 */
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.WorkbookFactory; // This is included in poi-ooxml-*.jar
import org.apache.poi.ss.usermodel.Workbook;
 
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 
public class PoiExample{
 
    public static void main(String[] args){
        try{
            // Create an Excel file.
            //***********************************
            Workbook writeWorkbook = new HSSFWorkbook();
            Sheet sheet1 = writeWorkbook.createSheet("new sheet");
 
            Row row1 = sheet1.createRow(0);
            Cell cell1 = row1.createCell(0);
            cell1.setCellValue("Xuan");
 
            // Write workbook to a file.
            FileOutputStream fileOut = new FileOutputStream("new_workbook.xls");
            writeWorkbook.write(fileOut);
            fileOut.close();
            writeWorkbook.close();
 
            // Read an Excel file.
            //***********************************
 
            // WorkbookFactory creates the appropriate kind of Workbook
            //    (HSSFWorkbook or XSSFWorkbook), by auto-detecting from the supplied file.
            Workbook readWorkbook = WorkbookFactory.create(new FileInputStream("new_workbook.xls") );
 
            // Get the first sheet.
            Sheet sheet = readWorkbook.getSheetAt(0);
 
            // Get the first cell.
            Row row = sheet.getRow(0);
            Cell cell = row.getCell(0);
 
            // Show what is being read.
            System.out.println("Value of cell(0,0) is "+cell.toString());
 
 
            // Close the workbook.
            readWorkbook.close();
        }
        catch(FileNotFoundException e){
            System.out.println(e);
        }
        catch(IOException e){
            System.out.println(e);
        }
 
    }
}

Output

Output result of creating an Excel file

Output of reading an Excel file

Github

  • https://github.com/xuanngo2001/java-apache-poi

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.