用jxl来读取excel单元格中的数据

 //读取excel文件拼接sql语句插入数据库
    public  void insertdata(String filepath,String tableName)
    {
        //引用jxl包中的wookbook 类来操作excel
        jxl.Workbook jwb=null;
        InputStream in=null;

        //读取excel文件
        try {
            in=new FileInputStream(filepath);
            jwb= Workbook.getWorkbook(in);     //从输入流中创建workbook对象
            Sheet rs=jwb.getSheet(0);              //读取第一个sheet表
            int rscolums=rs.getColumns();      //获取sheet表中的列数,下同
            int rsrows=rs.getRows();

            ……

                  Cell cell=rs.getCell(i,0);          //用于获取第一张sheet表中第1行的内容
               ……
                    Cell cell=rs.getCell(j,i);        //读取sheet表中第i行j列的单元格
                    String content=cell.getContents();       //存放取出来的单个单元格
                     System.out.println(content);
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (BiffException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }finally {
            if(jwb!=null)
                jwb.close();
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

    }

}

posted @ 2014-03-14 11:42  likyn  阅读(761)  评论(0编辑  收藏  举报