日期:2014-05-18  浏览次数:21031 次

JSP运用jxl读取excel问题
String path="D:\\Data\\Book1.xls";

InputStream is = new FileInputStream(path);

Workbook wb = Workbook.getWorkbook(is);

Sheet rs = wb.getSheet(0);

Cell cell = null;  

int columnCount=Sheet.getColumns();  
int rowCount=Sheet.getRows();  

for (int i = 0; i <rowCount; i++) {  
  for (int j = 0; j <columnCount; j++){  
  cell=sheet.getCell(j, i);  
  if(cell.getType()==CellType.NUMBER){  
  System.out.print(((NumberCell)cell).getValue());  
  }  
  else if(cell.getType()==CellType.DATE){  
  System.out.print(((DateCell)cell).getDate());  
  }  
  else{  
  System.out.print(cell.getContents());  
  }  


运行程序,提示

An error occurred at line: 8 in the jsp file: /test/test3.jsp
Generated servlet error:
Cannot make a static reference to the non-static method getColumns() from the type Sheet

An error occurred at line: 8 in the jsp file: /test/test3.jsp
Generated servlet error:
Cannot make a static reference to the non-static method getRows() from the type Sheet

An error occurred at line: 8 in the jsp file: /test/test3.jsp
Generated servlet error:
sheet cannot be resolved

应该是getColumns的method错掉了,但是正确的应该怎么写呢?请高手指教一下,不胜感激。

------解决方案--------------------
jxl.Workbook rwb = Workbook.getWorkbook(stream);
jxl.Sheet sh = rwb.getSheet(0);
int rowCount = sh.getRows();
for(int i=0;i<rowCounts;i++){
jxl.Cell[] ce = sh.getRow(i);
for(int j=0;j<ce.length;j++){
 String value=ce[i].getContents().toString();

}

}
你这样写看看。
------解决方案--------------------
Sheet.getColumns();Sheet.getRows();sheet.getCell(j, i); 
这些方法都不是static的,用你上边的Sheet对象rs,
都换成 rs.getColumns(); ……

------解决方案--------------------
public class ReadExl {
public static void main(String args[])
{
String path="c:\\测试数据2.xls";//Excel文件URL
try{
InputStream is = new FileInputStream(path);//写入到FileInputStream 
jxl.Workbook wb = Workbook.getWorkbook(is); //得到工作薄
jxl.Sheet st = wb.getSheet(0);//得到工作薄中的第一个工作表
//Cell cell=st.getCell(1,1);//得到工作表的第一个单元格,即A1 
//String content=cell.getContents();//getContents()将Cell中的字符转为字符串 
int row=st.getRows();
int col=st.getColumns();
for(int i=1;i<row;i++){
for(int j=0;j<col;j++){
Cell cell0 = st.getCell(j, i);//得到工作表的第一个单元格,即A1 
String content0 = cell0.getContents();
}
}
 
wb.close();//关闭工作薄
is.close();//关闭输入流
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(BiffException e){
e.printStackTrace();
}
}
这是我写的,你可以参照一下
------解决方案--------------------
An error occurred at line: 8 in the jsp file: /test/test3.jsp 
Generated servlet error: 
Cannot make a static reference to the non-static method getColumns() from the type Sheet 

An error occurred at line: 8 in the jsp file: /test/test3.jsp