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

开发中使用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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
/** * 此代码是完成从excel导入电话号码,将正确的电话号码保存到set集合,因为set集合对于重复的值会覆盖,所以达到了去重复的值的用例,并累计了不正确的电话号码的个数,对电话号码进行了验证有效性。所需要的 dom4j-1.6.1.jar;geronimo-stax-api_1.0_spec-1.0.jar;poi-3.7-20101029.jar;poi-ooxml-3.7-20101029.jar;poi-ooxml-schemas-3.7-20101029.jar;xmlbeans-2.3.0.jar; */ public static void main(String[] args) { Long errorMobileTotal=0L; // 保存正确的电话号码 Set<String> mobileSet = new HashSet<String>(); try { XSSFWorkbook wb = new XSSFWorkbook("E:/workbook1.xlsx"); XSSFSheet sheet = wb.getSheetAt(0); XSSFRow row = null; XSSFCell cell = null; String mobileStr=""; for (int i = 0; i <= sheet.getLastRowNum(); i++) { row = sheet.getRow(i); //System.out.print("第" + i + "行共" + row.getLastCellNum() +"列: "); for (int y = 0; y < row.getLastCellNum(); y++) { cell = row.getCell(y); // 设置字段为字符类型 cell.setCellType(XSSFCell.CELL_TYPE_STRING); // 判断储存格的格式 if (cell != null) { // 取得单元格的值 mobileStr = cell.getStringCellValue(); // 对手机号码进行验证身份正确 if(isMobileNO(mobileStr)) { // 保存正确的手机号码 mobileSet.add(mobileStr); System.out.println("号码"+mobileStr+"正确"); } else { // 累计不正确的电话号码的个数 errorMobileTotal++; System.out.println("不正确的电话号码个数:"+errorMobileTotal); System.out.println("号码"+mobileStr+"不正确"); } } // end (cell != null) }// end 遍历当前行 } // end 遍历当前工作单元sheet System.out.println("总共的行数:"+ (Long.valueOf(sheet.getLastRowNum())+1)); } catch (Exception e) { e.printStackTrace(); } // 因为要去除重复的所以可能有存在替换的字符 System.out.println("不正确的电话号码个数:"+errorMobileTotal); System.out.println("正确的电话号码个数:" + mobileSet.size()); } public static boolean isMobileNO(String mobiles){ Pattern p = Pattern.compile("^(\\+86)*0*((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"); Matcher m = p.matcher(mobiles); return m.matches(); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值