先下好jxl,导入jar包。以下代码是为了读取从第三行起的数据并存入到数据库中的测试代码(前两行是标题行)。存着供参考……
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","");
- String sql1 = "insert into student_info(CREATEDT,FULLNAME,BIRTHDAY,GENDER,MAJOR,REGION,INSTITUTION,MOBILEPHONE,QQNUMBER,EMAIL,FEE,ENTERFOREXTRA) value (?,?,?,?,?,?,?,?,?,?,?,?)";
- PreparedStatement ps1 = conn.prepareStatement(sql1);
- Workbook workbook = Workbook.getWorkbook(new File("d://EXCEL文件.xls"));
- Sheet sheet = workbook.getSheet(0);
- int columns = sheet.getColumns();
- int rows = sheet.getRows();
- for(int i=3;i<rows;i++){
- ps1.setString(1,sheet.getCell(1,i).getContents()); //CREATEDT 报名日期
- ps1.setString(2,sheet.getCell(2,i).getContents()); //FULLNAME 姓名
- ps1.setString(3,sheet.getCell(3,i).getContents()); //BIRTHDAY 出生日期
- ps1.setString(4, sheet.getCell(4,i).getContents()); //GENDER 性别
- ps1.setString(5, sheet.getCell(5,i).getContents()); //MAJOR 专业
- // 所在公司 6//
- // 所属岗位 7//
- // 学历 8//
- ps1.setString(6, sheet.getCell(9,i).getContents()); //REGION 所属地区
- ps1.setString(7, sheet.getCell(10,i).getContents()); //INSTITUTION 所属院校
- // 何时毕业 11 //
- ps1.setString(8, sheet.getCell(12,i).getContents()); //MOBILEPHONE 电话
- ps1.setString(9, sheet.getCell(13,i).getContents()); //QQNUMBER QQ
- ps1.setString(10, sheet.getCell(14,i).getContents()); //EMAIL 邮箱
- // 套餐名称 15
- String fee = sheet.getCell(16,i).getContents();
- if(fee!=""&&!"已退款".equals(fee)){ //FEE 收费情况
- ps1.setInt(11, Integer.parseInt(sheet.getCell(16,i).getContents()));
- }else{
- ps1.setInt(11,0);
- }
- ps1.setString(12, sheet.getCell(17,i).getContents()); //ENTERFOREXTRA 备注
- ps1.execute();
- }
- ps1.close();
- workbook.close();
- conn.close();
- } catch (BiffException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
其中 sheet.getCell(列,行).getContens()就可以获得相应单元格的内容。