Cell cell = row.getCell(column); if (cell != null){ if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ //判断是否为日期类型 if(HSSFDateUtil.isCellDateFormatted(cell)){ //用于转化为日期格式 Date d = cell.getDateCellValue(); DateFormat formater = new SimpleDateFormat("yyyy/MM/dd"); val = formater.format(d); }else{ // 用于格式化数字,只保留数字的整数部分 /*DecimalFormat df = new DecimalFormat("#");//转换成整型 val = df.format(cell.getNumericCellValue());*/ //cell.getNumericCellValue(); cell.setCellType(1); val = cell.getStringCellValue() + ""; }else if (cell.getCellType() == Cell.CELL_TYPE_STRING){ val = cell.getStringCellValue(); }else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){ val = cell.getCellFormula(); }else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){ val = cell.getBooleanCellValue(); }else if (cell.getCellType() == Cell.CELL_TYPE_ERROR){ val = cell.getErrorCellValue(); }catch (Exception e) { return val; return val; * 此代码是完成从 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+"正确"); // 累计不正确的电话号码的个数 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)如果 excel 表格中是 日期 格式 ,那么直接读取 row.getCell(7).getDateCellValue(); (2)如果 excel 表格中是字符串 格式 ,对字符串进行截取,并进行 格式 转化 TimeUtil.stringToFitDate(row.getCell(7).toString()); 工具类中的方法: public static Date stringToFitDate(String str) { //注意,这里要使用split方法,不能用截取固定
一、单元格 数据 类型 不对 应,报类型转换异常 因为row.getCell(0).getStringCellValue()指定的是取string类型,单元格中的 数据 可能是其他类型的 数据 ,所以取值时会出现类型转换错误; 解决方法: 在取值之前将类型强转为string r.getCell(0).setCellType(CellType.STRING); 二、单元格 数据 为空时,报空指针异常 这种就直接做一个判断就行 //判断是否为空,不为空则设置对应值,为空则设置null if(r.getCell(0) != null
1。首先我这边确认我 excel 手机号 码这个设置的类型是文本类型 2。但是在运行的时候 手机号 码还是被当成了number,所以解决方案怎么弄 为什么会这样呢:当使用POI处理 excel 的时候,遇到了比较长的数字,虽然 excel 里面设置该单元格是文本类型的,但是POI的cell的类型就会变成数字类型 原来cell.getCellType=0的时候 我是这样子写的 cellvalue=Strin
简单粗暴的方法 : DecimalFormat df=new DecimalFormat(“0”); Log.info(“序号:”+df.format(row.getCell(0).getNumericCellValue())); Log.info(“电话:”+df.format(row.getCell(4).getNumericCellValue())); 这个 问题 只解决了电话长数字的转换 问题 ,...
这个 问题 我研究了很久,在网上查了很多资料,大多数都是说编码方式 不对 应,或者编码方式本身 不对 。我把这些常见解决办法都试了一遍,都没有解决我的 问题 。先上 问题 ,如图所示: 第一张图是我的 Excel 表格 数据 第二张图是我 导入 数据 后的 乱码 图 可以看到,中文字段那里都 显示 的是问号!在这里我只说明我自己的 问题 ,若我解决的办法不能解决你的 问题 请见谅,你们自己再重新查找原因。 问题 所在: 我在 数据 库连接类里
Excel 中cell的内容可能是纯数字,可能是字符,可根据不同的方法来获取值。 对于纯数字,可用getNumericCellValue()获取 对于字符,可用getStringCellValue()获取 也可用setCellType(HSSFCell.CELL_TYPE_STRING)统一将cell中的内容当做字符串 但是这个方法过期了暂时没有找到替换的方法,可以用用 // 将区域编号的...
import java .util.Map; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class test { public static void main(String[] args) { //转化成 JSONObject obj = new JS...
可以使用 Apache POI 库来实现 Java 解析 Excel 文件的功能,并使用正则表达式来匹配 手机号 、姓名和省市区信息。 首先,你需要 导入 Apache POI 库,然后使用其提供的 API 来读取 Excel 文件中的 数据 。 然后,你可以使用正则表达式来匹配 手机号 和省市区信息,例如: 手机号 :使用正则表达式 `\d{11}` 来匹配11位数字,即 手机号 。 省市区:你可以使用第三方库或者公开 数据 源来获取省市区信息,然后使用正则表达式来匹配相应的信息。 最后,将匹配到的信息保存到一个 数据 结构中,例如一个 List 或者一个 Map。 以下是一个简单的示例代码,可以帮助你入门: ``` java import java .io.FileInputStream; import java .util.ArrayList; import java .util.List; import java .util.regex.Matcher; import java .util.regex.Pattern; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class Excel Parser { public static void main(String[] args) { try { // 读取 Excel 文件 Workbook workbook = WorkbookFactory.create(new FileInputStream("data.xlsx")); Sheet sheet = workbook.getSheetAt(0); // 定义正则表达式 Pattern phonePattern = Pattern.compile("\\d{11}"); Pattern areaPattern = Pattern.compile("上海市浦东新区"); // 定义 数据 结构 List<String> names = new ArrayList<>(); List<String> phones = new ArrayList<>(); List<String> areas = new ArrayList<>(); // 遍历所有行 for (Row row : sheet) { // 获取单元格 数据 Cell nameCell = row.getCell(0); Cell phoneCell = row.getCell(1); Cell areaCell = row.getCell(2); // 匹配 手机号 String phone = phoneCell.getStringCellValue(); Matcher phoneMatcher = phonePattern.matcher(phone); if (phoneMatcher.matches()) { phones.add(phone); // 匹配省市区 String area = areaCell.getStringCellValue(); Matcher areaMatcher = areaPattern.matcher(area); if (areaMatcher.matches()) { areas.add(area); // 匹配姓名 String name = nameCell.getStringCellValue(); if (!phoneMatcher.matches() && !areaMatcher.matches()) { names.add(name); // 输出结果 for (int i = 0; i < names.size(); i++) { System.out.println(names.get(i) + " " + phones.get(i) + " " + areas.get(i)); // 关闭 Excel 文件 workbook.close(); } catch (Exception e) { e.printStackTrace(); 注意:上面的代码示例是针对 xlsx 格式 Excel 文件,如果你要解析 xls 格式 的文件,则需要使用不同的 API。