2

My code is trying to Uploading the user data to an excel file. When user enter the data from the app for the first time, it writing the data to the excel file. But the problem came when the user try insert data for the second time. It just writing the same first line. Here is my code.

    public void LibrarianUpdateData(String Name,String IC,String Date){

    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet librarianSheet = wb.createSheet("Librarian");

    String[][] datatypes = {
            {Name,IC,Date}
    };

    int Rownum = librarianSheet.getLastRowNum();
    System.out.println(Rownum);
    System.out.println("Updating excel");
    System.out.println("Initial Array:");

    for(int i = 0 ; i < 3; i++){
        System.out.println(datatypes[0][i]);
    }

    for (String[] datatype : datatypes) {
        Row row = librarianSheet.createRow(Rownum++);
        int colNum = 0;
        for (String field : datatype) {
            Cell cell = row.createCell(colNum++);
            if (field instanceof String) {
                cell.setCellValue((String) field);
            }
        }
    }
    System.out.println(Rownum);
    try {

        // this Writes the workbook
        FileOutputStream out = new FileOutputStream(new File("AttendenceList.xlsx"));
        wb.write(out);
        out.close();
        System.out.println("AttendenceList.xlsx written successfully on disk.");
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

My brain says that the data writed the same line. Is my hypothesis right? How to alter this code so it write the next line?

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90

0 Answers0