What it is
Apache POI is a Java library for reading and writing Microsoft Office file formats, including Excel, Word, and PowerPoint. It enables Java applications to manipulate spreadsheets, documents, and presentations programmatically.
Apache POI provides APIs to read, write, and manipulate Microsoft Office files. For Excel, it supports HSSF (XLS) and XSSF (XLSX). For Word, it supports HWPF (DOC) and XWPF (DOCX). POI can create new documents, read existing files, update cell values, style sheets, and generate charts.
Installation
Add dependencies in pom.xml:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>Getting started
The smallest useful thing you can do with it, and what each part means.
import org.apache.poi.ss.usermodel.*;
import java.io.*;
FileInputStream fis = new FileInputStream("example.xlsx");
Workbook workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
workbook.close();
fis.close();Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Data");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("Name");
row.createCell(1).setCellValue("Age");
FileOutputStream fos = new FileOutputStream("output.xlsx");
workbook.write(fos);
fos.close();
workbook.close();Advanced usage
Where the library earns its place over a simpler alternative.
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
style.setFont(font);
row.getCell(0).setCellStyle(style);import org.apache.poi.xwpf.usermodel.*;
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Hello, Apache POI!");
FileOutputStream fos = new FileOutputStream("document.docx");
document.write(fos);
fos.close();
document.close();FileInputStream fis = new FileInputStream("document.docx");
XWPFDocument document = new XWPFDocument(fis);
for (XWPFParagraph para : document.getParagraphs()) {
System.out.println(para.getText());
}
document.close();
fis.close();import org.apache.poi.xslf.usermodel.*;
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFTextBox box = slide.createTextBox();
box.setText("Welcome to Apache POI PowerPoint");
FileOutputStream fos = new FileOutputStream("presentation.pptx");
ppt.write(fos);
fos.close();
ppt.close();Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- IOException
- Occurs when the file cannot be read or written. Ensure the file exists and you have proper permissions.
- EncryptedDocumentException
- Thrown when trying to open a password-protected Excel file without credentials. Provide a password or skip protected files.
- IllegalStateException: Cannot get a numeric value from a string cell
- Ensure you read cell types correctly; check `cell.getCellType()` before accessing content.
Best practices
- Always close Workbooks, Documents, and Streams to prevent resource leaks.
- Use XSSF for XLSX and HSSF for XLS to maintain compatibility.
- Leverage cell styles and formatting sparingly to improve performance.
- Validate file existence and handle IOException when reading or writing files.
- Use streaming APIs (SXSSF) for writing large Excel files efficiently.
Background
Why it exists, and what it was reacting to.
Apache POI was developed to allow Java developers to interact with Microsoft Office files without relying on COM or Microsoft APIs. It provides comprehensive support for both older binary formats (e.g., XLS, DOC) and newer OOXML formats (e.g., XLSX, DOCX). Widely used in enterprise applications, POI allows automation of reports, document generation, and data extraction.
