java - can not read last column data from excel file -


i reading data excel file salary generation in column empcode, emp_name,working_days,overtime_hour when reading data excel file read first 3 column means read working days can not read overtime_hour please suggest wrong.i tried bellow code

public class csv_upload extends jframe implements actionlistener     {          public static jframe f;         jpanel panel;         connection con = null;         jbutton b1,b2,b3,b4;           int status=0;         jtextfield txt1;         int company_id1=0;         jlabel l1;         font g,g1;           jfilechooser fc;           jtextarea log;           file file1 ;           string str;           jcombobox jb1;         public csv_upload()          {             panel=(jpanel)getcontentpane();             panel.setlayout(null);             g=new font("georgia",font.bold,22);             g1=new font("georgia",font.bold,15);             panel.setbackground(new java.awt.color(204, 230 , 255));                 l1=new jlabel("upload excel file");             l1.setbounds(200, 50, 400, 30);             l1.setfont(g);             l1.setforeground(color.red);             panel.add(l1);             jb1=loadcombobox();             jb1.setbounds(200, 100, 180,30);             jb1. additem("select company");             panel.add(jb1);             txt1=new jtextfield();             txt1.setbounds(540, 150,200,40);             panel.add(txt1);             fc = new jfilechooser();             b1=new jbutton("browse file");             b1.setbounds(30, 150,200,40);             b1.setfont(g1);             b1.setforeground(color.red);             panel.add(b1);             b2=new jbutton("upload file");             b2.setbounds(240, 150,200,40);             b2.setforeground(color.red);             b2.setfont(g1);             panel.add(b2);             imageicon img=new imageicon("calender.png");             b3=new jbutton(img);             b3.setbounds(460, 150,50,30);             b3.setforeground(color.red);             b3.setfont(g1);             panel.add(b3);             b1.addactionlistener(this);             b2.addactionlistener(this);             b3.addactionlistener(this);             jb1.additemlistener(new company_events());    }          @override         public void actionperformed(actionevent e)         {              if (e.getsource() == b1) {                   int returnval = fc.showopendialog(csv_upload.this);                    if (returnval == jfilechooser.approve_option) {                  file1 = fc.getselectedfile();                  str=string.valueof(file1);                     system.out.println("file fath"+file1);                   } else {                   }     }               if(e.getsource()==b2)              {                    if(!(txt1.gettext().isempty())&&company_id1!=0)                  {                  try                     {                          fileinputstream file = new fileinputstream(new file(str));                         system.out.println("action performed in file"+file);                          //create workbook instance holding reference .xlsx file                         xssfworkbook workbook = new xssfworkbook(file);                          //get first/desired sheet workbook                         xssfsheet sheet = workbook.getsheetat(0);                          //iterate through each rows 1 one                         iterator<row> rowiterator = sheet.iterator();                         while (rowiterator.hasnext())                          {                             row row = rowiterator.next();                             int a=0;                             int emp_code1=0;                             int emp_id = 0;                             double working_days=0,over_timehour = 0;                             tax_calculation tax=new tax_calculation();                             //for each row, iterate through columns                             iterator<cell> celliterator = row.celliterator();                              while (celliterator.hasnext())                              {                                 cell cell = celliterator.next();                                 //check cell type , format accordingly                                  switch (cell.getcelltype())                                  {                                       case cell.cell_type_numeric:                                         if(a==0)                                         {                                             emp_code1=(int) cell.getnumericcellvalue();                                             system.out.println("empcode"+emp_code1);                                         }                                         if(a==2)                                         {                                             working_days=cell.getnumericcellvalue();                                             system.out.println("working days"+working_days);                                         }                                         if(a==3)                                         {                                             over_timehour=cell.getnumericcellvalue();                                             system.out.println("ot hour"+   over_timehour);                                         }                                         a++;                                      break;                                     case cell.cell_type_string:     //                                  system.out.print(cell.getstringcellvalue() + "\t");     //                                  a++;                                         break;                                 }     //                          system.out.println("record find");     //                          system.out.println("value is"+cell.getcolumnindex());                             }                             system.out.println("");                             system.out.println("new rows");                             }                          file.close();                     }                        catch (exception e1)                      {                         e1.printstacktrace();                     }                      if(status>0)                     {                         joptionpane.showmessagedialog(null, "salary generated");                      }                     else                     {                         joptionpane.showmessagedialog(null,"salary not generated");                     }              }                   else                  {                      joptionpane.showmessagedialog(null,"please select comapny name , date ");                  }                }              if(e.getsource().equals(b3))                 {                 txt1.settext(new datepicker(f).setpickeddate());                 }               }          public jcombobox<string> loadcombobox()         {             preparedstatement st=null;             resultset res=null;              try {                 con = dbmsconnection.getconnection();                     jb1=new jcombobox<string>();                      string query="select * company_details";                     st =con.preparestatement(query);                     res=st.executequery();                     jb1.additem("please select company");                     while(res.next())                         {                             if(res.getstring(6).equalsignorecase("active"))                             {                                 jb1.additem(res.getstring(2));                             }                         }                 }             catch (sqlexception e)                  {                     e.printstacktrace();                 }                         {                 try                     {                         st.close();                         res.close();                         con.close();                     }                 catch(sqlexception ed)                     {                         ed.printstacktrace();                     }             }             return jb1;         }          class company_events implements itemlistener         {     preparedstatement stmt=null;     resultset res=null;              @override             public void itemstatechanged(itemevent e) {                  con=dbmsconnection.getconnection();                  if(e.getstatechange()==itemevent.selected)                 {                     system.out.println("selected company is"+string.valueof(jb1.getselecteditem()));                     try                     {                         stmt=con.preparestatement("select company_id company_details company_name=?");                         stmt.setstring(1, string.valueof(jb1.getselecteditem()));                         res=stmt.executequery();                         while(res.next())                         {                             company_id1=res.getint(1);                         }                      }catch (exception e5) {                         e5.printstacktrace();                     }                                         {                         try {                             stmt.close();                             res.close();                         } catch (sqlexception e1) {                             // todo auto-generated catch block                             e1.printstacktrace();                         }                      }                 }               }          }           public static void main(string []s)             {                  f=new csv_upload();                  f.setvisible(true);                 f.setsize(750,500);                     f.setdefaultcloseoperation(jframe.exit_on_close);             }     } 

i suspect column 1, employee name, non-numeric. a not incremented column , never equal 3. should observing value printed out "working days" overtime hours value in spreadsheet.

one solution use cell.getcolumnindex() instead of trying manually calculate column.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -