Append Data to Excel Example
This program demonstrates appending data into an existing Excel file.
Problem Statement
Write a Java program that appends a new row into an existing Excel file.
Source Code
| 1 | import java.io.*; |
| 2 | import org.apache.poi.ss.usermodel.*; |
| 3 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 4 | public class AppendExcelExample { |
| 5 | public static void main(String[] args) { |
| 6 | System.out.println("Eduinq Append Excel Example"); |
| 7 | try(FileInputStream fis=new FileInputStream("output.xlsx")) { |
| 8 | Workbook wb=new XSSFWorkbook(fis); |
| 9 | Sheet sheet=wb.getSheetAt(0); |
| 10 | int lastRow=sheet.getLastRowNum()+1; |
| 11 | Row row=sheet.createRow(lastRow); |
| 12 | row.createCell(0).setCellValue(3); |
| 13 | row.createCell(1).setCellValue("Rahul"); |
| 14 | row.createCell(2).setCellValue("UK"); |
| 15 | try(FileOutputStream fos=new FileOutputStream("output.xlsx")) { |
| 16 | wb.write(fos); |
| 17 | System.out.println("Eduinq Excel Appended Successfully"); |
| 18 | } |
| 19 | } catch(Exception e) { |
| 20 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 21 | } |
| 22 | } |
| 23 | } |
Program Output
Eduinq Append Excel Example Eduinq Excel Appended Successfully
Explanation
Appending uses getLastRowNum() to find next row index. - Adds new row and writes back to file. - Exception handling ensures safe operations. - In enterprise systems, appending Excel is used for incremental data updates. Conclusion: Shows appending data to Excel files.