您的位置:首页 > 编程语言 > Java开发

(个人笔记) java poi 解析excel

2013-04-24 13:11 477 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: