public boolean readExcel(File file){
boolean flag = true;
try {
//得到输入流
FileInputStream is = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(is);
//获得工作簿个数
int sheetNum = wb.getNumberOfSheets();
customerList = new ArrayList<CustomerBean>();
//遍历工作簿
for(int i=0; i<sheetNum; i++){
HSSFSheet childSeet = wb.getSheetAt(i);
//得到该工作簿数据行数
int rowNum = childSeet.getLastRowNum();
//遍历行,标题行除外
for(int j=1; j<= rowNum; j++){
//遍历一行所有单元格,获取单元格的值
HSSFRow row = childSeet.getRow(j);
//如果是空行,忽略
if(row == null || row.getLastCellNum()<=0){
continue;
}
String custName = getCellValue(row,0); // 客户姓名
String idenType = ""; // 证件类型
String itp = getCellValue(row,1);
String idenNum = getCellValue(row,2); // 证件号
String address = getCellValue(row,8); // 通讯地址
String tel = getCellValue(row,3); // 联系电话
String cpyName = getCellValue(row,6); // 单位名称
String postCode = getCellValue(row,4); // 邮政编码
//封装customer对象
customerBean = new CustomerBean();
customerBean.setCustName(custName);
customerBean.setIdenType(idenType);
customerBean.setIdenNum(idenNum);
customerBean.setAddress(address);
customerBean.setTel(tel);
customerBean.setCpyName(cpyName);
customerBean.setPostCode(postCode);
customerList.add(customerBean);
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("读取excel文件失败!");
flag = false;
}finally{
return flag;
}
}(个人笔记) java poi 解析excel
最新推荐文章于 2024-11-28 17:45:39 发布
本文介绍了一种使用Java从Excel文件中读取数据并将其封装为CustomerBean对象列表的方法。通过遍历工作表和行,提取客户姓名、证件号码等信息。
2860

被折叠的 条评论
为什么被折叠?



