package test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ExcelFileUpdateExample1 { public static void main(String[] args) { String excelFilePath = "JavaBooks.xlsx"; try { //FileInputStream inputStream = new FileInputStream(new File(excelFilePath)); OPCPackage pkg = null; try { pkg = OPCPackage.open(new File(excelFilePath));//inputStream); } catch (InvalidFormatException e) { e.printStackTrace(); } Workbook workbook = WorkbookFactory.create(pkg);//inputStream); Sheet sheet = workbook.getSheetAt(0); Object[][] bookData = { {"The Passionate Programmer", "Chad Fowler", 16}, {"Software Craftmanship", "Pete McBreen", 26}, {"The Art of Agile Development", "James Shore", 32}, {"Continuous Delivery", "Jez Humble", 41}, }; int rowCount = sheet.getLastRowNum(); for (Object[] aBook : bookData) { Row row = sheet.createRow(++rowCount); int columnCount = 0; Cell cell = row.createCell(columnCount); cell.setCellValue(rowCount); for (Object field : aBook) { cell = row.createCell(++columnCount); if (field instanceof String) { cell.setCellValue((String) field); } else if (field instanceof Integer) { cell.setCellValue((Integer) field); } } } //inputStream.close(); pkg.close(); FileOutputStream outputStream = new FileOutputStream("JavaBooks.xlsx"); workbook.write(outputStream); workbook.close(); outputStream.close(); } catch (IOException | EncryptedDocumentException ex) { ex.printStackTrace(); } } }