java中用jxl方式读取Excel

首先导入jxl的包,可以在网上下载

例如 读取一份存储有学生信息的Excel

public static List<Student> readExcel(String filePath){
  
  List<Student> students = new ArrayList<Student>();
    try { 
                 // 创建输入流,读取Excel 
                 InputStream is = new FileInputStream(filePath); 
                // jxl提供的Workbook类 
                Workbook wb = Workbook.getWorkbook(is);
                // Excel的页签数量 
                int sheet_size = wb.getNumberOfSheets(); 
                 for (int index = 0; index < sheet_size; index++) { 
                     // 每个页签创建一个Sheet对象 
                     Sheet sheet = wb.getSheet(index); 
                     // sheet.getRows()返回该页的总行数 
                     for (int i = 0; i < sheet.getRows(); i++) {  //代表行
                         // sheet.getColumns()返回该页的总列数 
                      Student  student = new Student();
                      //第一行为字段,所以跳过
                      if(i==0){
                       continue;
                      }
                       for (int j = 0; j < sheet.getColumns(); j++) {  //代表列
                             String cellinfo = sheet.getCell(j, i).getContents(); 
                            int  m =   sheet.getCell(j, i).getColumn();
                            switch(m){
                            case 0:
                             student.setNumber(Integer.parseInt(cellinfo));
                             break;
                            case 1:
                             student.setName(cellinfo);
                             break;
                            case 2:
                             student.setSex(cellinfo);
                             break;
                            case 3:
                             student.setAge(Integer.parseInt(cellinfo));
                             break;
                            case 4:
                             student.setAddress(cellinfo);
                             break;
                            }
                            // System.out.println(cellinfo); 
                         } 
                       students.add(student);
                     } 
                } 
          } catch (FileNotFoundException e) { 
                 e.printStackTrace(); 
             } catch (BiffException e) { 
                 e.printStackTrace(); 
             } catch (IOException e) { 
                 e.printStackTrace(); 
             }
             System.out.print(students.size());
  return students;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值