Working with Excel in Java: Library Overview and Practical Examples
In corporate Java development, there is often a need to programmatically create, read, and modify Excel spreadsheets. This can include generating reports, importing data from external systems, or exporting calculation results. In this article, we will review the main libraries for working with Excel in Java, their features, and provide clear code examples.
Why Java, not Python?
Despite our website being called PythonLib, Java remains one of the most popular languages for enterprise development. Many companies actively use Java for server-side applications, and integration with Excel is a common requirement. Understanding how to work with Excel in Java will be useful for both beginner and experienced developers.
Main Libraries for Working with Excel in Java
There are several mature libraries, each with its own strengths:
- Apache POI — the most popular and feature-rich library. Supports .xls (HSSF) and .xlsx (XSSF) formats. Allows creating, reading, and modifying files, supports formulas, styles, and charts.
- JExcelApi (jxl) — a lighter library that only supports the old .xls format. Suitable for simple tasks, but has not been developed since 2009.
- Apache POI + XSSF/SXSSF — advanced capabilities for working with large files. SXSSF (Streaming) allows processing millions of rows with low memory consumption.
- Docx4j — a library for working with Office Open XML, can be used for .xlsx, but is more commonly used for Word.
Getting Started with Apache POI
To add the library to a Maven project, add the following dependencies to pom.xml:
<!-- For working with .xls (HSSF) --><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.5</version></dependency>
<!-- For working with .xlsx (XSSF) --><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.5</version></dependency>If you use Gradle, add the lines to build.gradle:
implementation 'org.apache.poi:poi:5.2.5'implementation 'org.apache.poi:poi-ooxml:5.2.5'Example: Creating an Excel File with a Table
Let's look at a simple example of creating a .xlsx file with employee data.
import org.apache.poi.ss.usermodel.*;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream;import java.io.IOException;
public class ExcelWriter { public static void main(String[] args) { // Create a new workbook try (Workbook workbook = new XSSFWorkbook()) { // Create a sheet Sheet sheet = workbook.createSheet("Employees");
// Create a header row Row headerRow = sheet.createRow(0); String[] columns = {"ID", "Name", "Position", "Salary"}; for (int i = 0; i < columns.length; i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(columns[i]); // Make the header bold CellStyle style = workbook.createCellStyle(); Font font = workbook.createFont(); font.setBold(true); style.setFont(font); cell.setCellStyle(style); }
// Fill with data Object[][] data = { {1, "Ivan Ivanov", "Developer", 150000}, {2, "Petr Petrov", "Analyst", 120000}, {3, "Sidor Sidorov", "Manager", 180000}