(个人笔记) 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;
}
}
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;
}
}
相关文章推荐
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用POI解析Excel文件(二)
- java通过poi解析Excel文件,直接读取客户端文件
- java使用poi解析或处理excel的时候,如何防止数字变成科学计数法的形式
- Java基础系列19:使用JXL或者POI生成和解析Excel文件
- java使用poi解析或处理excel的时候,如何防止数字变成科学计数法的形式
- java基础入门-poi解析excel
- java POI搞excel笔记
- java通过poi解析excel中的数据
- java使用poi解析Excel遇到Cannot get a text value from a numeric cell
- Java POI 操作Excel总结笔记--使用配置和使用案例篇
- Java使用poi解析Excel
- java 利用POI 上传解析导出Excel 深入
- java使用poi解析或处理excel的时候,如何防止数字变成科学计数法的形式
- selenium 学习笔记 ---新手学习记录(10) 问题总结(java)--poi--excel 操作
- Java POI 操作Excel总结笔记--使用配置和使用案例篇
- java poi 创建XLS 用Jakarta POI操作Excel文件 POI创建 解析Excel
- java POI解析Excel 之数据转换公用方法(推荐)
- java使用poi解析或处理excel的时候,如何防止数字变成科学计数法的形式
- java poi 解析excel
- java使用poi解析或处理excel的时候,防止数字变成科学计数法的方法