使用POI是读取excel文件中电话号码及日期出现的问题及解决

来源:互联网 发布:数据库技术发展 编辑:程序博客网 时间:2024/05/16 17:44

开发中使用POI导入excel文件,中间有个单元格是电话号码,但是读取后有点问题,比如13200000000,变成1.3200000000E10,这个时候需要我们再去转换下

[java] view plain copy
  1. DecimalFormat format = new DecimalFormat("#");  
  2.   
  3.   
  4. Number value = cell.getNumbericCellValue();  
  5.   

  1. String phone = format.format(number);  






使用日期类型:

*万能处理方案

所有日期格式都可以通过getDataFormat()值来判断

yyyy-MM-dd-----14

yyyy年m月d日--- 31

yyyy年m月-------57

m月d日  ----------58

HH:mm-----------20

h时mm分  -------32

 

Java代码  收藏代码
  1. //1、判断是否是数值格式  
  2. if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){  
  3.     short format = cell.getCellStyle().getDataFormat();  
  4.     SimpleDateFormat sdf = null;  
  5.     if(format == 14 || format == 31 || format == 57 || format == 58){  
  6.         //日期  
  7.         sdf = new SimpleDateFormat("yyyy-MM-dd");  
  8.     }else if (format == 20 || format == 32) {  
  9.         //时间  
  10.         sdf = new SimpleDateFormat("HH:mm");  
  11.     }  
  12.     double value = cell.getNumericCellValue();  
  13.     Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);  
  14.     result = sdf.format(date);  
  15. }  



完整代码:
Workbook workbook = null;
int errorRow = -1;
int errorCell = -1;


// 从session域中获取商户ID
AppContext appContext = AppContext.getContext();
Long custmId = appContext.getSession("custId");


try {
workbook = new XSSFWorkbook(in);
} catch (Exception e) {
try {
workbook = new HSSFWorkbook(in);
} catch (IOException e1) {
return "导入失败,请检测格式。查考模板";
}
}


try {


Sheet sheet = null;
int count = workbook.getNumberOfSheets();
for (int i = 0; i < count; i++) {// 获取每个Sheet表
sheet = workbook.getSheetAt(i);
// 获取工作表的行数
int len = sheet.getPhysicalNumberOfRows();
for (int j = 1; j < len; j++) {// 获取每行


errorRow = j + 1;
Row row = sheet.getRow(j);
Fans fans = new Fans();


int cellCount = row.getLastCellNum();
for (int k = 0; k < cellCount; k++) {// 获取每个单元格
errorCell = k + 1;
String tempValue = null;
Cell cell = row.getCell(k);
if (cell == null)
continue;


if (k == 4) {
DecimalFormat format = new DecimalFormat("#");
Number value = cell.getNumericCellValue();
String phone = format.format(value);
tempValue = phone;
} else if (k == 6 || k == 7) {
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil
.getJavaDate(value);
tempValue = sdf.format(date);
} else {
tempValue = cell.getStringCellValue();
}


if (tempValue == null || tempValue == "") {
if (k == 4)
throw new PhoneNotNullException();
continue;
}
switch (k) {
case 0:
fans.setNickName(tempValue);
break;
case 1:
fans.setGender(tempValue);
break;
case 2:
fans.setProvinces(tempValue);
break;
case 3:
fans.setCity(tempValue);
break;
case 4:
fans.setPhoneNumber(tempValue);
break;
case 5:
fans.setStatus(Integer.parseInt(tempValue));
break;
case 6:
SimpleDateFormat formant = new SimpleDateFormat(
"yyyy-MM-dd");
Date date = formant.parse(tempValue);
fans.setLastInvitationTime(date);
break;
case 7:
SimpleDateFormat formant2 = new SimpleDateFormat(
"yyyy-MM-dd");
Date date2 = formant2.parse(tempValue);
fans.setAddTime(date2);
break;
case 8:
fans.setFansTags(tempValue);
break;
case 9:
fans.setWxAccount(tempValue);
break;
case 10:
fans.setSystemWxId(tempValue);
break;
case 11:
fans.setNote(tempValue);
break;
}
}
fans.setCustomId(custmId);
datas.add(fans);
successNum++;
}
}

0 0