0
votes

I created a registration form, that collects data from user, and display it into a Jtable, then I want to allow user to press a button to export the Jtable content to excel form. In the display table, it can show all user input, but when I export to excel, it only show columnNames and first row of data, so I want to know if I have done it wrong?


User input:

ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();

String[] columnNames = new String[] {"First Name", "Last Name", "Email", "Degree", "Year", "Event"};

Object[][] tabledata = new Object[1][6];  


    String fn = FirstName.getText();
    String ln = LastName.getText();
    String em = Email.getText();
    String re = ReEnter.getText();
    String de = Degree.getSelectedItem().toString();
    String yr = Year.getSelectedItem().toString();
    String ev = EventDate.getSelectedItem().toString();

    ArrayList<String> data = new ArrayList<String>();

    data.add(fn);
    data.add(ln);
    data.add(em);
    data.add(de);
    data.add(yr);
    data.add(ev);
    list.add(data);

    //Set fields to empty        
            FirstName.setText("");
            LastName.setText("");
            Email.setText("");
            ReEnter.setText("");
            Degree.setSelectedItem(null);
            Year.setSelectedItem(null);
            EventDate.setSelectedItem(null);

    // print data to table
            Object[][] temp = new Object[tabledata.length+1][6];
                for(int i=0;i<tabledata.length;i++){
                    for(int j=0;j<6;j++){
                        temp[i][j] = tabledata[i][j];
                    }
                temp[tabledata.length-1][0]= fn;
                temp[tabledata.length-1][1]= ln;
                temp[tabledata.length-1][2]= em;
                temp[tabledata.length-1][3]= de;
                temp[tabledata.length-1][4]= yr;
                temp[tabledata.length-1][5]= ev;
                }
            tabledata = temp;
            table.setModel(new DefaultTableModel(tabledata, columnNames));
                }
               }

Export data to excel:

   try {
        TableModel model = table.getModel();

        File file = new File("member.xls");
        FileWriter output = new FileWriter(file);


        for(int i = 0; i <model.getColumnCount(); i++){
            output.write(model.getColumnName(i) + "\t");
        }

        output.write("\n");

        for(int k=0;k<model.getRowCount();k++) {
            for(int j=0;j<model.getColumnCount();j++) {
                output.write(model.getValueAt(k,j).toString()+"\t");
            }
            output.write("\n");


        output.close();
    }
   }
    catch(Exception e) {
        e.printStackTrace();
    }

![Run program] [1]: http://i.stack.imgur.com/T99L5.png ![Exported excel file][2]: http://i.stack.imgur.com/ZGy3Z.png

1

1 Answers

0
votes

Check your bracketing :P

You call output.close() inside the first for-loop for printing out data. This closes the file before getting to the 2nd row and onwards.