Click here to Skip to main content
15,883,982 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to update data from the table to excel sheet.
Here is my code.

Java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:                                     
 try{
    JFileChooser fileChooser = new JFileChooser();
    int retval = fileChooser.showSaveDialog(jButton1);

    if (retval == JFileChooser.APPROVE_OPTION)
    {
        File file = fileChooser.getSelectedFile();
        if (file != null)
        
        {
            if (!file.getName().toLowerCase().endsWith(".xls")) 
            
            {
                file = new File(file.getParentFile(), file.getName() + ".xls");
            }

            try 
            {
                Excel exp=new Excel();
                exp.exportTable(jTable1, file);


                Desktop.getDesktop().open(file);
            } 
            catch (UnsupportedEncodingException e) 
            {} 
            catch (FileNotFoundException e) 
            {
                System.out.println("not found");
            } 
            catch (IOException e)
            {}
        }
   }


   }catch(Exception e){
       System.out.println("saved new data.");
   }
}     


//Excel class file

Java
class Excel
{
    
   public Excel()
    {}
    public void exportTable(JTable table, File file) throws IOException
    {
        TableModel model = table.getModel();
         FileWriter out = new FileWriter(file);
         BufferedWriter bw= new BufferedWriter(out);
         
         for(int i = 0; i < model.getColumnCount(); i++)
         { 
           
           bw.write(model.getColumnName(i) + "\t");
         }
         
        bw.write("\n");
        
        for(int i=0; i< model.getRowCount(); i++) {
         for(int j=0; j < model.getColumnCount(); j++) {
             
           bw.write(model.getValueAt(i,j).toString()+"\t");
        }
       bw.write("\n");
    }
     bw.close();
        System.out.println("Writing to" +file);
  }
}



I can open .xls file. but can't update data into it.
Please Guide me.
Thank you
Posted
Updated 4-Sep-14 18:34pm
v2

1 solution

use this library:

http://poi.apache.org/[^]

It's well supported and should work fine (it does for us!):

http://poi.apache.org/spreadsheet/index.html[^]
 
Share this answer
 
Comments
Kavi Sro 5-Sep-14 2:57am    
Thank you for your kind reply.
Kavi Sro 5-Sep-14 3:13am    
Thank you. Now I got my data from table displayed on excel sheet.
But still I cant update new data from table.

Please tell me,Is there any possible way to update data in netbeans itself?
I am working in linux platform
TorstenH. 5-Sep-14 7:22am    
netbeans is only your IDE that you are working on.
that has nothing to do with the tables in a excel file.

What do you mean by "I cant update new data from table" ? Please explain more detailed ( "improve question" ). Posting code often helps us to understand your task.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900