Working with Excel in Java: Library Overview and Code Examples
Java is one of the most popular languages for enterprise development, where there is often a need to interact with Microsoft Excel spreadsheets (formats .xls and .xlsx). Whether it's exporting reports, importing data from CRM, or automating accounting reports — a reliable tool for working with Excel is essential. In this article, we will review the main libraries for Java, their capabilities, advantages and disadvantages, and provide practical code examples.
Popular Libraries for Working with Excel in Java
Today, there are several mature solutions. The choice of a specific library depends on the Excel version, data volume, and required functionality.
- Apache POI — the de facto standard. Supports both formats (
.xlsand.xlsx), allows reading, writing, formatting cells, working with formulas, charts, and macros. Heavy, but the most powerful library. - JExcelApi (jxl) — a lighter alternative, but only supports the old
.xlsformat. Suitable for simple tasks where.xlsxsupport is not needed. - EasyExcel (Alibaba) — a lightweight version based on POI, optimized for working with large files and streaming processing. Excellent for importing/exporting millions of rows.
- OpenCSV — not directly for Excel, but for working with CSV (which Excel opens). Ideal for simple tabular data exchange without formatting.
Example 1: Reading an Excel File Using Apache POI
Let's consider a basic scenario: open the file data.xlsx and output the contents of the first sheet to the console.
import org.apache.poi.ss.usermodel.*;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileInputStream;import java.io.IOException;
public class ReadExcelExample { public static void main(String[] args) { String filePath = "data.xlsx";
try (FileInputStream fis = new FileInputStream(filePath); Workbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheetAt(0); // first sheet
for (Row row : sheet) { for (Cell cell : row) { switch (cell.getCellType()) { case STRING: System.out.print(cell.getStringCellValue() + "\\t"); break; case NUMERIC: System.out.print(cell.getNumericCellValue() + "\\t"); break; case BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\\t"); break; default: System.out.print("?\\t"); } } System.out.println(); } } catch (IOException e) { e.printStackTrace(); } }}Example 2: Writing Data to Excel with Formatting
Let's create a new file report.xlsx with headers and data, highlighting the headers in bold.
import org.apache.poi.ss.usermodel.*;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream;import java.io.IOException;
public class WriteExcelExample { public static void main(String[] args) { try (Workbook workbook = new XSSFWorkbook()) { Sheet sheet = workbook.createSheet("Report");
// Style for headers CellStyle headerStyle = workbook.createCellStyle(); Font headerFont = workbook.createFont(); headerFont.setBold(true); headerFont.setFontHeight Also in library
Working with YAML in JavaScript (Node.js): parsing, generation and validation
GraphQL in TypeScript: Libraries for Type-Safe APIs
OAuth client for Go (Golang): Connecting to OAuth 2.0 providers
WireMock.Net C# (.NET) Guide: Mock HTTP Servers & Examples