java poi 读取Excel中的手机号

本文介绍了一种利用Apache POI库处理Excel文件中手机号码的有效方法。通过使用DataFormatter类,可以将被视为double类型的手机号转换为正确的数字格式,避免科学计数法带来的困扰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用poi读取Excel手机号,通常会把它认为成double类型,然后使用科学计数法显示1.32....E10什么的,很苦恼。刚刚查了官方文档https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html,简单的很,代码如下:

使用DataFormatter格式化一下即可。

 1 package cn.gx.test;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 import java.util.logging.Level;
 9 import java.util.logging.Logger;
10 
11 import org.apache.poi.ss.usermodel.DataFormatter;
12 import org.apache.poi.xssf.usermodel.XSSFRow;
13 import org.apache.poi.xssf.usermodel.XSSFSheet;
14 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
15 
16 public class ExcelRead {
17 
18     public ExcelRead() {
19 
20     }
21 
22     public void testPoiExcel2007(InputStream ios) {
23         // 构造 XSSFWorkbook 对象,strPath 传入文件路径
24         XSSFWorkbook xwb = null;
25         try {
26             xwb = new XSSFWorkbook(ios);
27         } catch (IOException e) {
28             // TODO Auto-generated catch block
29             e.printStackTrace();
30         }
31         // 读取第一章表格内容
32         XSSFSheet sheet = xwb.getSheetAt(0);
33         // 定义 row、cell
34         XSSFRow row;
35         String cell;
36         // 循环输出表格中的内容
37         for (int i = sheet.getFirstRowNum(); i < sheet
38                 .getPhysicalNumberOfRows(); i++) {
39             row = sheet.getRow(i);
40             for (int j = row.getFirstCellNum(); j < row
41                     .getPhysicalNumberOfCells(); j++) {
42                 // 通过 row.getCell(j).toString() 获取单元格内容,
43                 int cellType = row.getCell(j).getCellType();
44                 // System.out.println("cellType:"+cellType);
45                 if (cellType == 0) {
46                     DataFormatter dataFormatter = new DataFormatter();
47                     dataFormatter.addFormat("###########", null);
48                     cell = dataFormatter.formatCellValue(row.getCell(j));
49                 } else {
50 
51                     cell = row.getCell(j).toString();
52                 }
53                 System.out.print(cell + "\t");
54             }
55             System.out.println("");
56         }
57     }
58 
59     public static void main(String[] args) {
60         String fileName = "D:\\user.xlsx";
61         File file = new File(fileName);
62         InputStream fileInpuStream = null;
63         try {
64             fileInpuStream = new FileInputStream(file);
65         } catch (FileNotFoundException e) {
66             // TODO Auto-generated catch block
67             e.printStackTrace();
68         }
69         // 检测代码
70         try {
71             ExcelRead er = new ExcelRead();
72             // 读取excel2007
73             er.testPoiExcel2007(fileInpuStream);
74         } catch (Exception ex) {
75             Logger.getLogger(ExcelRead.class.getName()).log(Level.SEVERE, null,
76                     ex);
77         }finally{
78             try {
79                 fileInpuStream.close();
80             } catch (IOException e) {
81                 // TODO Auto-generated catch block
82                 e.printStackTrace();
83             }
84         }
85     }
86 }

表格如下:两列,第一列姓名,第二列手机号码

usernameuserphone
wang13270893332
zhang15651892525
song15651236542

控制台输出:

username userphone
wang 13270893332
zhang 15651892525
song 15651236542

 

 

有些东西不是你不会,而是你不知道还有其他解决方案

转载于:https://www.cnblogs.com/lucky2u/p/3914977.html

/** * 此代码是完成从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(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值