Apache POI is a powerful library that enables Java developers to interact with Microsoft Office file formats, such as Word, Excel, and PowerPoint. The library is open-source and actively maintained by the Apache Software Foundation. In this comprehensive guide, we’ll cover the core features of Apache POI, installation, and examples to work with different file formats.
Apache POI is a Java API that provides means to read, write, and manipulate Microsoft Office file formats. The name “POI” stands for “Poor Obfuscation Implementation,” reflecting the fact that the file formats were originally difficult to work with due to their obfuscation.
The main components of Apache POI include:
.xls
)..xlsx
)..doc
)..docx
)..ppt
)..pptx
).To get started with Apache POI, you’ll need to add the necessary dependencies to your Java project. If you’re using Maven, add the following to your pom.xml
file:
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
If you’re using Gradle, add the following to your build.gradle
file:
dependencies {
implementation 'org.apache.poi:poi:5.1.0'
implementation 'org.apache.poi:poi-ooxml:5.1.0'
}
To create a new Excel workbook, use the XSSFWorkbook
class for .xlsx
files or the HSSFWorkbook
class for .xls
files. Here’s an example:
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelExample {
public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
// ...
}
}
Apache POI provides a simple API to read and write data to Excel cells. Here’s an example of how to create a new sheet, add some data, and save the file:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelReadWriteExample {
public static void main(String[] args) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello, Apache POI!");
try (FileOutputStream outputStream = new FileOutputStream("example.xlsx")) {
workbook.write(outputStream);
}
workbook.close();
}
}
Apache POI provides a CellStyle
interface to apply various cell formatting options, such as borders, alignment, and number formats. Here’s an example of how to set the cell background color and apply a border:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelFormattingExample{
public static void main(String[] args) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Formatted Cell");
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderTop(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
cell.setCellStyle(style);
try (FileOutputStream outputStream = new FileOutputStream("formatted_example.xlsx")) {
workbook.write(outputStream);
}
workbook.close();
}
}
Apache POI provides the XWPFDocument
class to work with Word documents (.docx). Here’s an example of creating a new Word document, adding a paragraph, and saving the file:
import org.apache.poi.xwpf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class WordExample {
public static void main(String[] args) throws IOException {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Hello, Apache POI!");
try (FileOutputStream outputStream = new FileOutputStream("example.docx")) {
document.write(outputStream);
}
document.close();
}
}
Apache POI provides the XMLSlideShow
class to work with PowerPoint presentations (.pptx). Here’s an example of creating a new PowerPoint presentation, adding a slide with a title, and saving the file:
import org.apache.poi.xslf.usermodel.*;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import java.io.IOException;
public class PowerPointExample {
public static void main(String[] args) throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);
XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE);
XSLFSlide slide = ppt.createSlide(titleLayout);
XSLFTextShape title = slide.getPlaceholder(0);
title.setText("Hello, Apache POI!");
try (FileOutputStream outputStream = new FileOutputStream("example.pptx")) {
ppt.write(outputStream);
}
ppt.close();
}
}
Apache POI is a powerful library to interact with Microsoft Office file formats in Java. This guide covered the basics of working with Excel, Word, and PowerPoint files, but there are many more features available, such as reading and modifying existing files, working with charts, images, and much more. To explore further, refer to the official Apache POI documentation.