can not read last column data from excel file

fanky

I am reading data from Excel file for the salary generation in that column is empcode, emp_name,working_days,overtime_hour when reading data from excel file i am read only first three column means i am read only working days i can not read overtime_hour please suggest what i 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 to .xlsx file
                        XSSFWorkbook workbook = new XSSFWorkbook(file);

                        //Get first/desired sheet from the workbook
                        XSSFSheet sheet = workbook.getSheetAt(0);

                        //Iterate through each rows one by 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 all the columns
                            Iterator<Cell> cellIterator = row.cellIterator();

                            while (cellIterator.hasNext()) 
                            {
                                Cell cell = cellIterator.next();
                                //Check the cell type and 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 is 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 is generated");

                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null,"Salary is not Generated");
                    }
             }

                 else
                 {
                     JOptionPane.showMessageDialog(null,"Please Select Comapny name and 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 * from 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();
                }
            finally
            {
                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 from company_details where 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();
                    }
                    finally
                    {
                        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);
            }
    }
Russell Zahniser

I suspect that column 1, employee name, is non-numeric. Thus your a is not incremented for that column and is never equal to 3. You should be observing that the value printed out for "working days" is actually the overtime hours value in the spreadsheet.

One solution would be to use cell.getColumnIndex() instead of trying to manually calculate the column.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Gnuplot read line style from data file column

From Dev

Can I read new data from an open file without reopening it?

From Dev

ClosedXML read from excel file

From Dev

Read date column from Excel

From Dev

sails:How can i read excel file data into json format

From Dev

How to read data from data base or excel file in Nightwatch?

From Dev

How can I read each column separately from a tsv data file?

From Dev

Can't read property of length undefined for last value read from file

From Dev

add last column to the file from another file

From Dev

Read data from a table in .MDB file into Excel 2013 with VBA

From Dev

Can't read data from hash using column(:) in rails

From Dev

Open and read from excel file

From Dev

Iterately read sheets from excel file and modify column names in Python

From Dev

Validate Data that is being read from an Excel File with EP Plus

From Dev

Read data from Excel file in Selenium Java

From Dev

How To Read data from Microsoft Excel File by C++ Program

From Dev

Gnuplot read line style from data file column

From Dev

Can I read new data from an open file without reopening it?

From Dev

How can i delete last column from my text file

From Dev

How can I read the data from text file and load it into the vector?

From Dev

Read date column from Excel

From Dev

sails:How can i read excel file data into json format

From Dev

How can a read-only purpose data connection to MS Excel/Access from MS Excel prevent the MS Access file from being locked to edits?

From Dev

Can't read data from hash using column(:) in rails

From Dev

Open and read from excel file

From Dev

Read from excel file error

From Dev

Excel vba to autofill formulae from a column to last column in excel

From Dev

How to read data from csv file if all the values are in the same column?

From Dev

Read data from excel file Laravel Excel

Related Related

  1. 1

    Gnuplot read line style from data file column

  2. 2

    Can I read new data from an open file without reopening it?

  3. 3

    ClosedXML read from excel file

  4. 4

    Read date column from Excel

  5. 5

    sails:How can i read excel file data into json format

  6. 6

    How to read data from data base or excel file in Nightwatch?

  7. 7

    How can I read each column separately from a tsv data file?

  8. 8

    Can't read property of length undefined for last value read from file

  9. 9

    add last column to the file from another file

  10. 10

    Read data from a table in .MDB file into Excel 2013 with VBA

  11. 11

    Can't read data from hash using column(:) in rails

  12. 12

    Open and read from excel file

  13. 13

    Iterately read sheets from excel file and modify column names in Python

  14. 14

    Validate Data that is being read from an Excel File with EP Plus

  15. 15

    Read data from Excel file in Selenium Java

  16. 16

    How To Read data from Microsoft Excel File by C++ Program

  17. 17

    Gnuplot read line style from data file column

  18. 18

    Can I read new data from an open file without reopening it?

  19. 19

    How can i delete last column from my text file

  20. 20

    How can I read the data from text file and load it into the vector?

  21. 21

    Read date column from Excel

  22. 22

    sails:How can i read excel file data into json format

  23. 23

    How can a read-only purpose data connection to MS Excel/Access from MS Excel prevent the MS Access file from being locked to edits?

  24. 24

    Can't read data from hash using column(:) in rails

  25. 25

    Open and read from excel file

  26. 26

    Read from excel file error

  27. 27

    Excel vba to autofill formulae from a column to last column in excel

  28. 28

    How to read data from csv file if all the values are in the same column?

  29. 29

    Read data from excel file Laravel Excel

HotTag

Archive