e6621887 发表于 2013-2-4 19:11:32

poi导入Excel

               
    FileInputStream filein=new FileInputStream("bbc.xls");    得到bbc.xls的输入流,
    POIFSFileSystem fs=new POIFSFileSystem(filein);            从bbc.xls读
    HSSFWorkbook wb=new HSSFWorkbook(fs);                     工作薄
//    POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("c:\\aa.xls"));
       HSSFSheet sheet=wb.getSheetAt(0);                   里面的工作表,第1个工作表
       for (int i=0;i<7;i++)
       {
       HSSFRow row=sheet.getRow(i);              这里是读第1行,,这里做个循环读七行。。后面跟据这个row读列
       System.out.println("i="+i);
       if (row==null)continue;                  这里最好加row==null continue   因为如果他读到null不加就会报错后面。。
      
           for (int j=1;j<7;j++)                 读七列
           {
             
               if (row.getCell((short)j)==null)         读到null    continue跳 出
               {
                   System.out.println("j="+j); 
                   continue;
               }
               else if(row.getCell((short)j).getStringCellValue().trim().equals("name:"))   这里就是读到名字,就取他后面的值
               {
                   int n=j+1;
                   System.out.println("name="+row.getCell((short)n).getStringCellValue().trim());  n就是列,
                   tf1.setText(row.getCell((short)n).getStringCellValue().trim());      把它设置到TextField里面去
                  
               }
               else if(row.getCell((short)j).getStringCellValue().trim().equals("age:"))    同上,但这里注意读数字要getNumericCellValue()
               {                                                                                    要不就会报异常
                   int n=j+1;
                   System.out.println("age="+row.getCell((short)n).getNumericCellValue());
                   tf2.setText(String.valueOf(row.getCell((short)n).getNumericCellValue()));
                  
               }
               else
               {
                   System.out.println(row.getCell((short)j).getStringCellValue().trim());
               }
           }
       }
      
     
          filein.close();
          System.out.println("aaa");
            }
            catch(Exception ex)
            {
            System.out.println("error   "+ex.toString());
            }

        }
    }
页: [1]
查看完整版本: poi导入Excel