Language: Java
Data / Office
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.
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.
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>implementation 'org.apache.poi:poi:5.2.3'
implementation 'org.apache.poi:poi-ooxml:5.2.3'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.
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();Reads all rows and cells from the first sheet of an Excel file and prints the content.
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();Creates a new Excel file with a header row containing 'Name' and 'Age'.
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
style.setFont(font);
row.getCell(0).setCellStyle(style);Applies bold font styling to a cell in Excel.
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();Creates a new Word document with a paragraph of text.
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();Reads and prints all paragraphs from an existing Word document.
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();Creates a new PowerPoint presentation with a single slide and text box.
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.