gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:09:26 26 4
gpt4 key购买 nike

我正在从 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);
}
}

最佳答案

我怀疑第 1 列(员工姓名)不是数字。因此,您的 a 不会针对该列递增,并且永远不会等于 3。您应该观察到,打印出的“工作日”值实际上是电子表格中的加类时间值。

一种解决方案是使用 cell.getColumnIndex()而不是尝试手动计算列。

关于java - 无法从 excel 文件中读取最后一列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23055288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com