您的位置:程序门 -> java -> web 开发



jsp运用jxl读取excel问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


jsp运用jxl读取excel问题
发表于:2007-10-29 23:25:41 楼主
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错掉了,但是正确的应该怎么写呢?请高手指教一下,不胜感激。
发表于:2007-10-29 23:31:001楼 得分:0
尝试把getcolumns和getrows换成具体数字,getcells又报错。应该怎么解决啊?高手帮帮忙啊。。谢谢。。。
发表于:2007-10-30 08:36:052楼 得分:0
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();

}

}
你这样写看看。
发表于:2007-10-30 14:19:073楼 得分:0
sheet.getcolumns();sheet.getrows();sheet.getcell(j,   i);  
这些方法都不是static的,用你上边的sheet对象rs,
都换成       rs.getcolumns();   ……
发表于:2007-10-30 14:34:294楼 得分:0
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();
            }
        }
这是我写的,你可以参照一下
发表于:2007-10-30 14:47:235楼 得分:0
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()和getrows()不是静态方法,所以不能直接通过类来调用吗?
第三个大概是包没有引入。
发表于:2007-10-30 14:51:106楼 得分:0
楼上正解
发表于:2007-10-31 16:47:567楼 得分:0
请问5楼高手,这个问题静态方法的问题应该怎么解决呢?我看得懂问题,但不知道怎么解决。。

请问3楼高手,我的代码已经换成如下:
<%
try{

resultset   rs;
resultset   rs1;

string   path="d:\\data\\book1.xls";

inputstream   is   =   new   fileinputstream(path);

workbook   wb   =   workbook.getworkbook(is);

sheet   rs1   =   wb.getsheet(0);

cell   cell   =   null;      

int   columncount=rs.getcolumns();      
int   rowcount=rs.getrows();      

for   (int   i   =   0;   i   <rowcount;   i++)   {      
        for   (int   j   =   0;   j   <columncount;   j++){      
                cell=rs.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());      
                }      
}
}
rs.close();
rs   =   null;
}
finally{

}

%>

现在的错误是
an   error   occurred   at   line:   8   in   the   jsp   file:   /test/test3.jsp
generated   servlet   error:
resultset   cannot   be   resolved   to   a   type

an   error   occurred   at   line:   8   in   the   jsp   file:   /test/test3.jsp
generated   servlet   error:
resultset   cannot   be   resolved   to   a   type

an   error   occurred   at   line:   8   in   the   jsp   file:   /test/test3.jsp
generated   servlet   error:
duplicate   local   variable   rs1

应该是resultset   declare   错误了。。还有rs1重复使用,但是应该怎么问题呢?   如果是写成rs.getcolumns()..
rs要怎么declare?谢谢各位高手大大指教了。。。
发表于:2007-11-01 15:22:098楼 得分:0
哪位高手可以帮忙啊?小弟感激不尽。。
发表于:2007-11-03 15:59:529楼 得分:0
求助啊。。。。有人可以帮忙吗?5555。。。不胜感激。。。
发表于:2007-11-03 16:28:4710楼 得分:0
三楼的是正解.开始,是因为 你从sheet对象中调用getrowns(),这是一个静态方法的写法,而使用的方法是非静态的.所以会报在非静态方法引用了一个静态的方法.
sheet       rs       =       wb.getsheet(0);  

cell       cell       =       null;              

int       columncount=sheet.getcolumns();           //这个sheet应该换成rs.

然而你在七楼的代码是因为在同一个方法内不同类型的对象都使用了同一个变量名.
resultset       rs;  
resultset       rs1;   //此处是java.sql.reslutset

string       path=   "d:\\data\\book1.xls   ";  

inputstream       is       =       new       fileinputstream(path);  

workbook       wb       =       workbook.getworkbook(is);  

sheet       rs1       =       wb.getsheet(0);   //而此处是sheet.
把变量换一下,另外导入所要的包,就没有问题了.看你出的错,好像是刚学java吧!
发表于:2007-11-04 19:36:3111楼 得分:0
对啊。。的确是刚学。。。没办法。。都没教过。。只好到处上网查找。。希望大家帮忙哈。
发表于:2007-11-04 19:57:5212楼 得分:0
最后的问题,请各位大大帮忙。。不胜感激。。

代码如下,运行以后发现没有报错,可是也没有内容显示出来,整个ie浏览器是白板。。。这是什么原因造成的啊?我明明excel里面有内容的啊。。

ps:怎么给大家加分数啊?谢谢帮忙了。。

<%
try{


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=rs.getcolumns();      
int   rowcount=rs.getrows();      

for   (int   i   =   0;   i   <rowcount;   i++)   {      
        for   (int   j   =   0;   j   <columncount;   j++){      
                cell=rs.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());      
                }      
}
}
rs   =   null;
}
finally{

}

%>
发表于:2007-11-04 19:59:4513楼 得分:0
jxl的java包已经放到\webapps\root\web-inf\lib下面了啊。。。


快速检索

最新资讯
热门点击