处理表格数据


	/**
	 * 处理表格数据
	 */
	public static Map<String, Object> processAttendeeExcel(String filePathOrFileName, InputStream inputStream,
			Map<String, String> distinctFiled, Map<String, Object> filedCode,List<String> citys,List<String> states,Workbook workBook) throws Exception {
		
		System.out.println("/************************************去除重复的字段****************************************/"+distinctFiled);
		Map<String, Object> result = new HashMap<>();
		
		//{手机=memberPhone, 公司=companyName, 参会方式=way, 邀请来源=utmSource, 职位=postionCode, 
		//姓名=memberName, 邮箱=memberEmail, 省/直辖市=province, 子行业=industryLv2Code, 城市=city,
		//	与本公司关系=companyRelationCode}
		//  行业=industryLv1Code, 
		//  fileList=[姓名, 手机, 邮箱, 省/直辖市, 城市, 公司, 职位, 行业, 子行业, 与本公司关系, 邀请来源, 参会方式], 
		filedCode.put("与华为的关系", "companyRelationCode");
		// 基本参数
		String fileType = filePathOrFileName.substring(filePathOrFileName.lastIndexOf(".") + 1,
				filePathOrFileName.length());
		InputStream is = null;
		// 通过判断值
		try {

			if (null == inputStream) {
				is = new FileInputStream(filePathOrFileName);
			} else {
				is = inputStream;
			}
			// 放置导入表格中所有的字段名
			List<String> titleNames = new ArrayList<String>();
			List<String> titleCodes = new ArrayList<>();	//放置所有的标题对应的英文
			List<String> proTitleNames = new ArrayList<String>();
			List<String> proTitleCodes = new ArrayList<String>();
			LinkedHashMap<String, String> columnNameMap = new LinkedHashMap<>();// 存放将来可能用到的表头
			
//			// 正常,异常数
//			Integer successTotal = 0;
//			Integer failureTotal = 0;
			// 数据集合,第一页sheet数据
			List<Map<String, Object>> sheetList = new ArrayList<Map<String, Object>>();// 对应sheet页
			//对省份、城市、邮箱、手机校验的错误信息放在这里
			List<Map<String, String>> checkResultList=new ArrayList<>();
			// 取第一页
			Sheet sheet = workBook.getSheetAt(0);
			sheet.removeColumnBreak(1);
			// 去除导入excel中未配置的列数据
			List<String> filterFields = new ArrayList<>();
			int rowSize = sheet.getLastRowNum() + 1;
			//导入数据中手机号码集合
			List<String> phoneList = new ArrayList<String>();
			//导入数据中邮箱集合
			List<String> emailList = new ArrayList<String>();
			for (int j = 0; j < rowSize; j++) {// 遍历行
				Row row = sheet.getRow(j); // 行对象
				Map<String, Object> rowMap = new LinkedHashMap<String, Object>();// 对应一行数据
				if (row == null) {// 略过空行的内容设置
					// sheetList.add(rowMap); //将空行的行号记录纳入数据List中
					continue;
				}
				int cellSize = row.getLastCellNum();// 行中有多少个单元格,也就是有多少列               //11
				if (j == 0) {
					// 第一行标题处理
					for (int k = 0; k < cellSize; k++) {
						Cell cell = row.getCell(k);
						String title = (null != cell) ? cell.toString() : "";
						titleNames.add(title);
						titleCodes.add((String) filedCode.get(title));
						if(filedCode.get(title) != null) {
							columnNameMap.put((String) filedCode.get(title), title);
						}
						if (!StringUtil.isEmpty(filedCode.get(title)+"")) {
							proTitleNames.add(title);
							proTitleCodes.add(filedCode.get(title)+"");
						} else {
							// 根据表单中没有的值,剔除多余的数据
							filterFields.add(k + "");
						}

					}
				} else {
					// 数据行处理
					List<Integer> errorCode = new ArrayList<Integer>();
					StringBuffer buffer = new StringBuffer();
					//非空字段
					List<String> disList = new ArrayList<String>();
					disList.add("姓名");
					disList.add("手机");
					disList.add("邮箱");
					disList.add("省/直辖市");
					disList.add("城市");
					for (int k = 0; k < titleNames.size(); k++) {
						String errorMsg = buffer.toString();
						if (filterFields.contains(k + "")) {
							continue;
						}
						// 获取列
						Cell cell = row.getCell(k);
						String key = titleNames.get(k);
						String value = null;
						if (cell != null) {
//							value = processValue(cell);
							value = ExcelUtil.getCellValue(cell);
						}
						if(value == null && disList.contains(key)) {
							if(StringUtil.isEmpty(errorMsg)) {
								buffer.append(key+"为空");
							}else {
								buffer.append(","+key+"为空");
							}
							Integer code = null;
							switch (key) {
							case "姓名":
								code = 0;
								break;
							case "手机":
								code = 1;
								break;
							case "邮箱":
								code = 2;
								break;
							case "省/直辖市":
								code = 3;
								break;
							case "城市":
								code = 4;
								break;
							default:
								break;
							}
							if(null != code) {
								errorCode.add(code);
							}
							continue;
						}
						if(null == value) {
							continue;
						}else {
							value = value.trim();
						}
						//errorCode对应值  0:姓名为空    1:手机号格式不对    2:邮箱格式不正确   3:省份格式不对   4:城市格式不对   
						//对姓名进行校验
						if (null!=key&&"姓名".equals(key)) {
							boolean flag = value != null;
							if (!flag) {
								if(StringUtil.isEmpty(errorMsg)) {
									buffer.append("姓名为空");
								}else {
									buffer.append(",姓名为空");
								}
								errorCode.add(0);
							}
						}
						//对手机进行格式校验
						if (null!=key&&"手机".equals(key)) {
							phoneList.add(value);
//							boolean flag=value!=null&&value.matches("^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$");
							
							boolean flag=value!=null&&value.matches("^[1](([3|5|8][\\d])|([4][4,5,6,7,8,9])|([6][2,5,6,7])|([7][^9])|([9][1,8,9]))[\\d]{8}$");
							if (!flag) {
								if(StringUtil.isEmpty(errorMsg)) {
									buffer.append("手机号码格式不正确");
								}else {
									buffer.append(",手机号码格式不正确");
								}
								errorCode.add(1);
							}
						}
						//对邮箱进行格式校验
						if (null!=key&&"邮箱".equals(key)) {
							emailList.add(value);
							boolean flag = value != null && value.matches("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");  
							if (!flag) {
								if(StringUtil.isEmpty(errorMsg)) {
									buffer.append("邮箱格式不正确"); 
								}else {
									buffer.append(",邮箱格式不正确");
								}
								errorCode.add(2);
							}
						}
						//对省份进行格式性校验
						if (null!=key&&"省/直辖市".equals(key)) {
							if (!states.contains(value)) {
								if(StringUtil.isEmpty(errorMsg)) {
									buffer.append("省/直辖市格式不正确");
								}else {
									buffer.append(",省/直辖市格式不正确");
								}
								errorCode.add(3);
							}
						}
						//对城市进行存在性校验
						if (null!=key&&"城市".equals(key)) {
							if (!citys.contains(value)) {
								if(StringUtil.isEmpty(errorMsg)) {
									buffer.append("城市格式不正确");
								}else {
									buffer.append(",城市格式不正确");
								}
								errorCode.add(4);
							}
						}
						rowMap.put(filedCode.get(key)+"", value);

					}
					rowMap.put("errorCode", errorCode);
					rowMap.put("errorMsg", buffer.toString());
//					if(rowMap.containsKey("memberPhone") && rowMap.containsKey("memberName")) {
					if(rowMap.containsKey("memberName")) {
						sheetList.add(rowMap);
					}
				}
			}

			result.put("data", sheetList);//[{memberName=去去去, memberPhone=, memberEmail=573964656@qq.com, companyName=123, postionCode=456, industryLv1Code=789, industryLv2Code=789, province=广东省, city=深圳市, way=测试, utmSource=测试, status=success}]
			result.put("titleNames", proTitleNames);//[姓名, 手机, 邮箱, 公司, 职位, 行业, 子行业, 省/直辖市, 城市, 参会方式, 邀请来源]
			result.put("titleCodes", proTitleCodes);//[memberName, memberPhone, memberEmail, companyName, postionCode, industryLv1Code, industryLv2Code, province, city, way, utmSource]
			result.put("columnNameMap", columnNameMap);//{memberName=姓名, memberPhone=手机, memberEmail=邮箱, companyName=公司, postionCode=职位, industryLv1Code=行业, industryLv2Code=子行业, province=省/直辖市, city=城市, way=参会方式, utmSource=邀请来源}
			result.put("total", rowSize-1);
			result.put("phoneList", phoneList);
			result.put("emailList", emailList);
			return result;

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} finally {

			if (is != null) {
				workBook.close();
				is.close();
			}

		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值