Writing Your First Excel File
Harry
· 24 Sep 2026
· 1 views
Log in to track your progress and mark lessons complete.
Sponsored
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.");Key Points
- Always use try-with-resources: both
Workbookand the output stream must be closed. - Rows and cells are 0-indexed.
setCellValueis overloaded forString,double,boolean,DateandCalendar.- Full runnable version:
WriteExcelExamplein the poi-examples repo.