简体   繁体   English

无法从excel文件中读取最后一列数据

[英]can not read last column data from excel file

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我正在从 Excel 文件中读取该列中工资生成的数据是 empcode, emp_name,working_days,overtime_hour 从 excel 文件中读取数据时,我只读取前三列意味着我只读取工作日我无法读取 overtime_hour 请建议什么我错了。我试过下面的代码

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);
            }
    }

I suspect that column 1, employee name, is non-numeric.我怀疑第 1 列员工姓名是非数字的。 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.因此,您的a不会为该列增加并且永远不会等于 3。您应该观察到为“工作日”打印的值实际上是电子表格中的加班时间值。

One solution would be to use cell.getColumnIndex() instead of trying to manually calculate the column.一种解决方案是使用cell.getColumnIndex()而不是尝试手动计算列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM