String filePath = "path/to/excel/file.xlsx";
try {
FileInputStream fileInputStream = new FileInputStream(new File(filePath));
Workbook workbook = WorkbookFactory.create(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
String cellValue = cell.getStringCellValue();
System.out.println(cellValue);
}
}
workbook.close();
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello, World!");
String filePath = "path/to/save/excel/file.xlsx";
try {
FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath));
workbook.write(fileOutputStream);
workbook.close();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>