请教一下使用java操作excel时sheet.getColumns函数为何无法正常使用
package work2;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class mainwork {
public String sheetname=null;
public JLabel result=null;
public int rownum=0;
public int columnum=0;
public JFrame f=null;
public Sheet sheet=null;
public Sheet[] sheets=new Sheet[100];
private JButton check=new JButton("检查");
final JTextField name=new JTextField(20);
final JTextField month=new JTextField(2);
final Workbook book = Workbook.getWorkbook(new File("E:\\work\\test.xls"));
mainwork()throws Exception, IOException {
f = new JFrame("统计查询系统");
f.setLayout(null);
f.setSize(1000,600);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
JLabel lab=new JLabel("请输入需要查询的零件名称");
JLabel lab2=new JLabel("请输入需要查询的月份");
f.add(lab);
f.add(lab2);
lab.setBounds(100, 20, 200, 20);
lab2.setBounds(100, 150, 200, 20);
f.add(name);
f.add(month);
name.setBounds(100, 60, 50, 20);
month.setBounds(100, 190, 50, 20);
f.add(check);
check.setBounds(100, 500, 100, 50);
check.addActionListener(new mylistener());
book.close();
}
class mylistener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
mainwork.this.sheetname=mainwork.this.name.getText();
mainwork.this.sheets=book.getSheets();
for(int x=0;x<sheets.length;x++){
if(sheets[x].getName().equals(sheetname))
sheet=sheets[x];
}
for (int i = 1; i <sheet.getColumns(); i++){
System.out.println("hehe");
result=new JLabel();
int count=0;
for(int j=1;j<sheet.getRows()-1;j++){// 循环进行读写
Cell cell1 = sheet.getCell(i,j );
if(sheet.getCell(i,j).getContents().isEmpty()){
}
else{
if(Integer.parseInt(sheet.getCell(0,j).getContents().substring(3, 5))==Integer.parseInt(month.getText()))
count=count+Integer.parseInt(cell1.getContents());
}
}
result.setText(sheet.getCell(i,0).getContents()+" "+count);
result.setBounds(500,100+50*i,100,50);
}
f.add(result);
}
}
public static void main(String args[]) throws IOException, Exception{
new mainwork();
}
}
还是个学生刚开始学java 这个作业中发现运行时sheet.getColumns());函数会无法使用 但测试发现sheet已经成功读取到工作表
而且如果把这个函数放到监听外面可以正常使用 很不理解 请问哪里出了错误 如何修改