Liny_@NotePad

沉迷ACG中

使用jxl组件读取excel

YOYO posted @ 2008年11月13日 23:11 in 【Java SE】 with tags jxl excel , 2692 阅读

先下好jxl,导入jar包。以下代码是为了读取从第三行起的数据并存入到数据库中的测试代码(前两行是标题行)。存着供参考……

  1.                 try {
  2.                         Class.forName("com.mysql.jdbc.Driver");
  3.                        
  4.                         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","");
  5.                        
  6.                         String sql1 = "insert into student_info
  7. (CREATEDT,FULLNAME,BIRTHDAY,GENDER,MAJOR,REGION,INSTITUTION,
  8. MOBILEPHONE,QQNUMBER,EMAIL,FEE,ENTERFOREXTRA)
  9. value (?,?,?,?,?,?,?,?,?,?,?,?)";
  1.                         PreparedStatement ps1 = conn.prepareStatement(sql1);
  2.                        
  3.                         Workbook workbook = Workbook.getWorkbook(new File("d://EXCEL文件.xls"));
  4.                        
  5.                         Sheet sheet = workbook.getSheet(0);
  6.                         int columns = sheet.getColumns();
  7.                         int rows = sheet.getRows();
  8.                        
  9.                         for(int i=3;i<rows;i++){
  10.                                 ps1.setString(1,sheet.getCell(1,i).getContents());            //CREATEDT    报名日期
  11.                                 ps1.setString(2,sheet.getCell(2,i).getContents());            //FULLNAME    姓名
  12.                                 ps1.setString(3,sheet.getCell(3,i).getContents());            //BIRTHDAY    出生日期
  13.                                 ps1.setString(4, sheet.getCell(4,i).getContents());          //GENDER     性别
  14.                                 ps1.setString(5, sheet.getCell(5,i).getContents());          //MAJOR            专业
  15.                                 //      所在公司 6//
  16.                                 //      所属岗位 7//
  17.                                 //      学历         8//       
  18.                                 ps1.setString(6, sheet.getCell(9,i).getContents());          //REGION     所属地区
  19.                                 ps1.setString(7, sheet.getCell(10,i).getContents());    //INSTITUTION       所属院校
  20.                                 //      何时毕业 11 //
  21.                                 ps1.setString(8, sheet.getCell(12,i).getContents());    //MOBILEPHONE       电话
  22.                                 ps1.setString(9, sheet.getCell(13,i).getContents());    //QQNUMBER    QQ
  23.                                 ps1.setString(10, sheet.getCell(14,i).getContents());   //EMAIL        邮箱
  24.                                 //      套餐名称 15
  25.                                 String fee = sheet.getCell(16,i).getContents();
  26.                                 if(fee!=""&&!"已退款".equals(fee)){        //FEE          收费情况
  27.                                         ps1.setInt(11, Integer.parseInt(sheet.getCell(16,i).getContents()));
  28.                                 }else{
  29.                                         ps1.setInt(11,0);
  30.                                 }
  31.                                 ps1.setString(12, sheet.getCell(17,i).getContents());   //ENTERFOREXTRA    备注
  32.                                
  33.                                 ps1.execute();
  34.                         }
  35.                        
  36.                         ps1.close();
  37.                         workbook.close();
  38.                         conn.close();
  39.                 } catch (BiffException e) {
  40.                         e.printStackTrace();
  41.                 } catch (IOException e) {
  42.                         e.printStackTrace();
  43.                 } catch (ClassNotFoundException e) {
  44.                         e.printStackTrace();
  45.                 } catch (SQLException e) {
  46.                         e.printStackTrace();
  47.                 }

其中 sheet.getCell(列,行).getContens()就可以获得相应单元格的内容。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter