Writing Your First Excel File

Harry · 24 Sep 2026 · 1 views
Log in to track your progress and mark lessons complete.

XSSFWorkbook in 10 Lines

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;

try (Workbook wb = new XSSFWorkbook()) {
    Sheet sheet = wb.createSheet("Employees");
    Row header = sheet.createRow(0);
    header.createCell(0).setCellValue("Name");
    header.createCell(1).setCellValue("Salary");
    Row r = sheet.createRow(1);
    r.createCell(0).setCellValue("Nisha");
    r.createCell(1).setCellValue(75000);
    try (FileOutputStream out = new FileOutputStream("employees.xlsx")) {
        wb.write(out);
    }
}
System.out.println("employees.xlsx created.");

Writing rows and cells with XSSF

Key Points

  • Always use try-with-resources: both Workbook and the output stream must be closed.
  • Rows and cells are 0-indexed.
  • setCellValue is overloaded for String, double, boolean, Date and Calendar.
  • Full runnable version: WriteExcelExample in the poi-examples repo.
Share this post:

Comments (0)

Please login or register to comment.

Create a free account to keep reading

You've enjoyed a free tutorial! Register (it's free) to unlock every tutorial, track your progress and save code.

or sign in with your account

Already have an account? Log in