2.8K Star 9.2K Fork 4K

GVP卓源软件 / JeeSite

 / 详情

Excel导入的时候 titile字段起不到匹配的作用

待办的
创建于  
2017-07-27 17:28

目前的逻辑是根据字段顺序导入的,而字段中注解的 ExcelFli

分析了一下解决了这个问题 代码如下

Row titleRow = this.getRow(headerNum);
		
		List<E> dataList = Lists.newArrayList();
		for (int i = this.getDataRowNum(); i <= this.getLastDataRowNum(); i++) {
			E e = (E)cls.newInstance();
			int column = 0;
			Row row = this.getRow(i);
			StringBuilder sb = new StringBuilder();
			
			for (Object[] os : annotationList){
				
				ExcelField ef = (ExcelField)os[0];
				//获取标题的列数
				int cloumNum = titleRow.getPhysicalNumberOfCells();
				for(int j=0;j<cloumNum;j++){
					//判断当前列标题字段与当前被注解字段的title是否一致  如果一致则改变colum的值
					if(titleRow.getCell(j).getStringCellValue().equals(ef.title())){
						column = j;
						break;
					}
				}
				
				Object val = this.getCellValue(row, column);
				
				if (val != null){
					
					// If is dict type, get dict value
					if (StringUtils.isNotBlank(ef.dictType())){
						if(val.toString().contains(",")){
							String[] dictLabelArray = val.toString().split(",");
							//val = "";
							for(String dictLabel : dictLabelArray){
								val = val + DictUtils.getDictValue(dictLabel, ef.dictType(), "") + ",";
							}
						}
						else{
							val = DictUtils.getDictValue(val.toString(), ef.dictType(), "");
						}
						//log.debug("Dictionary type value: ["+i+","+colunm+"] " + val);
					}
					// Get param type and type cast
					Class<?> valType = Class.class;
					if (os[1] instanceof Field){
						valType = ((Field)os[1]).getType();
					}else if (os[1] instanceof Method){
						Method method = ((Method)os[1]);
						if ("get".equals(method.getName().substring(0, 3))){
							valType = method.getReturnType();
						}else if("set".equals(method.getName().substring(0, 3))){
							valType = ((Method)os[1]).getParameterTypes()[0];
						}
					}
					//log.debug("Import value type: ["+i+","+column+"] " + valType);
					try {
						if (valType == String.class){
							String s = String.valueOf(val.toString());
							if(StringUtils.endsWith(s, ".0")){
								val = StringUtils.substringBefore(s, ".0");
							}else{
								val = String.valueOf(val.toString());
							}
						}else if (valType == Integer.class){
							val = Double.valueOf(val.toString()).intValue();
						}else if (valType == Long.class){
							val = Double.valueOf(val.toString()).longValue();
						}else if (valType == Double.class){
							val = Double.valueOf(val.toString());
						}else if (valType == Float.class){
							val = Float.valueOf(val.toString());
						}else if (valType == Date.class){
							val = DateUtil.getJavaDate((Double)val);
						}else{
							if (ef.fieldType() != Class.class){
								val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString());
							}else{
								val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(), 
										"fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString());
							}
						}
					} catch (Exception ex) {
						log.info("Get cell value ["+i+","+column+"] error: " + ex.toString());
						val = null;
					}
					// set entity value
					if (os[1] instanceof Field){
						Reflections.invokeSetter(e, ((Field)os[1]).getName(), val);
					}else if (os[1] instanceof Method){
						String mthodName = ((Method)os[1]).getName();
						if ("get".equals(mthodName.substring(0, 3))){
							mthodName = "set"+StringUtils.substringAfter(mthodName, "get");
						}
						Reflections.invokeMethod(e, mthodName, new Class[] {valType}, new Object[] {val});
					}
				}
				sb.append(val+", ");
			}

评论 (1)

不能自已 创建了任务

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(1)
Java
1
https://gitee.com/thinkgem/jeesite.git
git@gitee.com:thinkgem/jeesite.git
thinkgem
jeesite
JeeSite

搜索帮助