java利用excel导入用户数据

try {
    FileInputStream inputStream = new FileInputStream(new File(tempPath+fileName));
    boolean is03Excel = fileName.matches("^.+\\.(?i)(xls)$");
    //1.读取工作簿
    Workbook workbook = is03Excel?new HSSFWorkbook(inputStream):new XSSFWorkbook(inputStream);
    //2.读取工作表
    Sheet sheet = workbook.getSheetAt(0);
    //3.读取行
    //判断行数大于二,是因为数据从第三行开始插入
    if(sheet.getPhysicalNumberOfRows() > 2)
    {
        Employee user = null;
        //跳过前两行
        for(int k=2;k<sheet.getPhysicalNumberOfRows();k++ )
        {
            //读取单元格
            Row row0 = sheet.getRow(k);
            user = new Employee();
            //用户名
            Cell cell0 = row0.getCell(0);
            user.setEmpId((int)cell0.getNumericCellValue());
            //账号
            Cell cell1 = row0.getCell(1);
            user.setEmpName(cell1.getStringCellValue());
            //所属部门
            Cell cell2 = row0.getCell(2);
            user.setEmail(cell2.getStringCellValue());
            //设置性别
            Cell cell3 = row0.getCell(3);
            String gender = "男".equals(cell3.getStringCellValue())?"M":"F";
            user.setGender(gender);

            Cell cell4 = row0.getCell(4);
            user.setDId((int)cell4.getNumericCellValue());

            employeeService.saveEmp(user);
        }
        File file=new File(tempPath+fileName);
        if(file.exists()&&file.isFile())
            file.delete();
    }
    workbook.close();
    inputStream.close();

} catch (Exception e) {
    e.printStackTrace();
}
阅读更多
换一批

没有更多推荐了,返回首页