Backend Development 11 min read

Using Apache POI to Create and Read Excel Files in Java

This article introduces Apache POI, the Java API for Microsoft Office documents, explains its core components such as Workbook, Sheet, Row, and Cell, and provides detailed code examples for creating, reading, and converting Java object lists to Excel files, including Maven dependencies and annotation usage.

Top Architect
Top Architect
Top Architect
Using Apache POI to Create and Read Excel Files in Java

Apache POI is a Java library for handling Microsoft Office documents, most commonly used for Excel manipulation. The article first explains what POI is and shows the Maven dependencies needed (poi and poi‑ooxml version 3.9).

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>

The library provides two main implementations for Excel files: HSSF for the older .xls format (Excel 97‑2003) and XSSF for the newer .xlsx format (Excel 2007+).

2. POI Core Classes

The central abstraction is Workbook (package org.apache.poi.ss.usermodel ), with two concrete classes:

HSSFWorkbook – works with .xls files.

XSSFWorkbook – works with .xlsx files.

Sheets are represented by HSSFSheet and XSSFSheet , both implementing the Sheet interface. Rows are HSSFRow / XSSFRow , and cells are HSSFCell / XSSFCell .

// Create a new sheet
workbook.createSheet();
// Create a named sheet
workbook.createSheet("SheetName");

// Create a row
Sheet sheet = workbook.getSheetAt(0);
sheet.createRow(0);

// Create a cell
Row row = sheet.getRow(0);
row.createCell(0);
row.createCell(0, CellType.STRING);

3. Creating and Reading Workbooks

Example of creating an empty workbook and writing it to a file:

import java.io.*;
import org.apache.poi.xssf.usermodel.*;

public class CreateWorkBook {
    public static void main(String[] args) throws Exception {
        // Create Blank workbook
        XSSFWorkbook workbook = new XSSFWorkbook();
        // Create file output stream
        FileOutputStream out = new FileOutputStream(new File("createworkbook.xlsx"));
        // Write workbook to file
        workbook.write(out);
        out.close();
        System.out.println("createworkbook.xlsx written successfully");
    }
}

Example of opening an existing workbook:

import java.io.*;
import org.apache.poi.xssf.usermodel.*;

public class OpenWorkBook {
    public static void main(String args[]) throws Exception {
        File file = new File("openworkbook.xlsx");
        FileInputStream fIP = new FileInputStream(file);
        XSSFWorkbook workbook = new XSSFWorkbook(fIP);
        if (file.isFile() && file.exists()) {
            System.out.println("openworkbook.xlsx file open successfully.");
        } else {
            System.out.println("Error to open openworkbook.xlsx file.");
        }
    }
}

4. Converting a List of Objects to Excel

A utility method is shown that converts a generic List<E> into an Excel sheet. It uses a custom @Excel annotation to specify sheet and column names.

@Excel(name = "学生标签页")
public class Student {
    @Excel(name = "姓名")
    private String name;
    private boolean male;
    @Excel(name = "身高")
    private int height;
    // getters and setters omitted for brevity
}

Test code that builds a list of Student objects and writes them to 1314.xls :

public static void main(String[] args) {
    List
list = new ArrayList<>();
    Student s1 = new Student();
    s1.setName("小红");
    s1.setMale(false);
    s1.setHeight(167);
    Student s2 = new Student();
    s2.setName("小明");
    s2.setMale(true);
    s2.setHeight(185);
    list.add(s1);
    list.add(s2);
    File file = new File("C:/Users/Dulk/Desktop/1314.xls");
    createExcel(list, file);
}

The ExcelUtil class contains the implementation of createExcel , handling reflection to read annotations, generate header rows, and fill cell values.

public class ExcelUtil {
    private static Logger log = Logger.getLogger(ExcelUtil.class);
    public static Workbook createExcel(List
list, File file) {
        // ... reflection logic to read @Excel annotations ...
        // write workbook to file
    }
    // other helper methods omitted for brevity
}

The article also includes promotional text and QR‑code images, but the core technical content focuses on using Apache POI for Excel operations in Java.

JavamavenAnnotationsExcelApache POIworkbook
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.