心有余而发不足 2016-08-23 12:02 采纳率: 25%
浏览 1528
已采纳

POI导入excel时Method failed for object异常怎么办?

图片说明
// 导入excel文件
public void importExcel(File userExcel, String userExcelFileName) {
try {
FileInputStream fileInputStream = new FileInputStream(userExcel);
boolean is03Excel = userExcelFileName.matches("^.+\.(?i)(xls)$");
//1、读取工作簿
Workbook workbook = is03Excel ? new HSSFWorkbook(fileInputStream):new XSSFWorkbook(fileInputStream);
//2、读取工作表
Sheet sheet = workbook.getSheetAt(0);
//3、读取行
if(sheet.getPhysicalNumberOfRows() > 2){
User user = null;
for(int k = 2; k < sheet.getPhysicalNumberOfRows(); k++){
//4、读取单元格
Row row = sheet.getRow(k);
user = new User();
//用户名
Cell cell0 = row.getCell(0);
user.setUserName(cell0.getStringCellValue());
//帐号
Cell cell1 = row.getCell(1);
user.setAccount(cell1.getStringCellValue());
//所属部门
Cell cell2 = row.getCell(2);
user.setDeptName(cell2.getStringCellValue());
//性别
Cell cell3 = row.getCell(3);
user.setGender(cell3.getStringCellValue().equals("男"));
//手机号
String mobile = "";
Cell cell4 = row.getCell(4);
try {
mobile = cell4.getStringCellValue();
} catch (Exception e) {
double dMobile = cell4.getNumericCellValue();
mobile = BigDecimal.valueOf(dMobile).toString();
}
user.setPhone(mobile);

                //电子邮箱
                Cell cell5 = row.getCell(5);
                user.setEmail(cell5.getStringCellValue());
                //生日
                Cell cell6 = row.getCell(6);
                if(cell6.getDateCellValue() != null){
                    user.setBirth(cell6.getDateCellValue());
                }
                //默认用户密码为 123456
                user.setPassWord("123456");
                //默认用户状态为 有效
                user.setState(User.USER_STATE_VALID);

                //5、保存用户
                save(user);
            }
        }
        workbook.close();
        fileInputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  • 写回答

3条回答 默认 最新

  • 心有余而发不足 2016-08-23 15:50
    关注

    其实是环境的问题,有些网友说是数据类型的转换错误导致的方法操作失败,也有可能。我用的是POI3.11,支持的JDK版本在1.6及其以后,myeclipse10内置的是jdk不建议使用,tomcat6也不建议,我换成环境jdk1.6.45,tomcat7.0就okle .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!