diff --git a/src/main/java/com/thinkgem/jeesite/common/utils/excel/ExportExcel.java b/src/main/java/com/thinkgem/jeesite/common/utils/excel/ExportExcel.java index bb1ff6d3fa6911fe225d28350ab853ba198cf6c0..60d3f400cc438f24dc3c91aba26b39547ac7483f 100644 --- a/src/main/java/com/thinkgem/jeesite/common/utils/excel/ExportExcel.java +++ b/src/main/java/com/thinkgem/jeesite/common/utils/excel/ExportExcel.java @@ -161,7 +161,94 @@ public class ExportExcel { } initialize(title, headerList); } - + + /** + * 重载 + * 实体对多业务导出(根据业务导出字段),使用模式匹配 + * 例如: 对用户的导出:带密码导出 + * 属性get方法注解带有 bussinessType = "密码" 都会导入 + * @param bussniessType 支持对实体的多业务不同字段导出 + * @param title + * @param cls + * @param type + * @param groups + */ + public ExportExcel(String bussniessType,String title, Class cls, int type, int... groups){ + // Get annotation field + Field[] fs = cls.getDeclaredFields(); + for (Field f : fs){ + ExcelField ef = f.getAnnotation(ExcelField.class); + if (ef != null && (ef.type()==0 || ef.type()==type)) { + if( ef.bussinessType().toString().contains( bussniessType)){ //模糊匹配支持一个属性支撑多个业务 xuelongjiang@jiadakeji.cn + if (groups != null && groups.length > 0) { + boolean inGroup = false; + for (int g : groups) { + if (inGroup) { + break; + } + for (int efg : ef.groups()) { + if (g == efg) { + inGroup = true; + annotationList.add(new Object[]{ef, f}); + break; + } + } + } + } else { + annotationList.add(new Object[]{ef, f}); + } + } + } + } + // Get annotation method + Method[] ms = cls.getDeclaredMethods(); + for (Method m : ms){ + ExcelField ef = m.getAnnotation(ExcelField.class); + if (ef != null && (ef.type()==0 || ef.type()==type)) { + if( ef.bussinessType().toString().contains( bussniessType)){//模糊匹配支持一个属性支撑多个业务 xuelongjiang@jiadakeji.cn + if (groups != null && groups.length > 0) { + boolean inGroup = false; + for (int g : groups) { + if (inGroup) { + break; + } + for (int efg : ef.groups()) { + if (g == efg) { + inGroup = true; + annotationList.add(new Object[]{ef, m}); + break; + } + } + } + } else { + annotationList.add(new Object[]{ef, m}); + } + } + } + } + // Field sorting + Collections.sort(annotationList, new Comparator() { + public int compare(Object[] o1, Object[] o2) { + return new Integer(((ExcelField)o1[0]).sort()).compareTo( + new Integer(((ExcelField)o2[0]).sort())); + }; + }); + // Initialize + List headerList = Lists.newArrayList(); + for (Object[] os : annotationList){ + String t = ((ExcelField)os[0]).title(); + // 如果是导出,则去掉注释 + if (type==1){ + String[] ss = StringUtils.split(t, "**", 2); + if (ss.length==2){ + t = ss[0]; + } + } + headerList.add(t); + } + initialize(title, headerList); + } + /** * 构造函数 * @param title 表格标题,传“空值”,表示无标题 diff --git a/src/main/java/com/thinkgem/jeesite/common/utils/excel/ImportExcel.java b/src/main/java/com/thinkgem/jeesite/common/utils/excel/ImportExcel.java index d6a54a95d6c8f7a945e532fb8d7e941896114770..6b69a6d9bc7fd3e4c8920a8af5e309d37dc5d020 100644 --- a/src/main/java/com/thinkgem/jeesite/common/utils/excel/ImportExcel.java +++ b/src/main/java/com/thinkgem/jeesite/common/utils/excel/ImportExcel.java @@ -9,10 +9,8 @@ import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.List; +import java.math.BigDecimal; +import java.util.*; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -192,6 +190,12 @@ public class ImportExcel { if (cell != null){ if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ val = cell.getNumericCellValue(); + //处理数字为科学计数,及日期正确转换为字符 xuelongjiang@jiadakeji.cn + val = cell.getNumericCellValue(); + if(val.toString().contains("E")){ + BigDecimal bd = new BigDecimal(val.toString()); + val = bd.toPlainString(); + } }else if (cell.getCellType() == Cell.CELL_TYPE_STRING){ val = cell.getStringCellValue(); }else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){ @@ -277,6 +281,20 @@ public class ImportExcel { E e = (E)cls.newInstance(); int column = 0; Row row = this.getRow(i); + //判断行是否为空 xuelongjiang@jiadakeji.cn + Iterator cellIterable = row.cellIterator(); + int cellCount = 0; + int cellEmpty = 0; + while (cellIterable.hasNext()){ + Cell itCell = cellIterable.next(); + cellCount++; + if("".equals(itCell.toString())){ + cellEmpty++; + } + } + if(cellCount == cellEmpty){ + continue; + } StringBuilder sb = new StringBuilder(); for (Object[] os : annotationList){ Object val = this.getCellValue(row, column++); @@ -349,6 +367,175 @@ public class ImportExcel { return dataList; } + /** + * 重载getDataList + * 实体对多业务导出(根据业务导出字段),使用模式匹配 + * 例如: 对用户的导出:带密码导出 + * 属性get方法注解带有 bussinessType = "密码" 都会导入 + * + * @param bussinessType 业务场景 使用字符 模糊匹配 如 + * @param cls + * @param groups + * @param + * @return + * @throws InstantiationException + * @throws IllegalAccessException + */ + public List getDataList(String bussinessType,Class cls, int... groups) throws InstantiationException, IllegalAccessException{ + List annotationList = Lists.newArrayList(); + // Get annotation field + Field[] fs = cls.getDeclaredFields(); + for (Field f : fs){ + ExcelField ef = f.getAnnotation(ExcelField.class); + if (ef != null && (ef.type()==0 || ef.type()==2)){ + if( ef.bussinessType().toString().contains( bussinessType)){ //模糊匹配支持一个属性支撑多个业务 xuelongjiang@jiadakeji.cn + if (groups!=null && groups.length>0) { + boolean inGroup = false; + for (int g : groups) { + if (inGroup) { + break; + } + for (int efg : ef.groups()) { + if (g == efg) { + inGroup = true; + annotationList.add(new Object[]{ef, f}); + break; + } + } + } + } + }else{ + annotationList.add(new Object[]{ef, f}); + } + } + } + // Get annotation method + Method[] ms = cls.getDeclaredMethods(); + for (Method m : ms){ + ExcelField ef = m.getAnnotation(ExcelField.class); + if (ef != null && (ef.type()==0 || ef.type()==2)){ + if( ef.bussinessType().toString().contains( bussinessType)){ //模糊匹配支持一个属性支撑多个业务 + if (groups!=null && groups.length>0) { + boolean inGroup = false; + for (int g : groups) { + if (inGroup) { + break; + } + for (int efg : ef.groups()) { + if (g == efg) { + inGroup = true; + annotationList.add(new Object[]{ef, m}); + break; + } + } + } + } + }else{ + annotationList.add(new Object[]{ef, m}); + } + } + } + // Field sorting + Collections.sort(annotationList, new Comparator() { + public int compare(Object[] o1, Object[] o2) { + return new Integer(((ExcelField)o1[0]).sort()).compareTo( + new Integer(((ExcelField)o2[0]).sort())); + }; + }); + //log.debug("Import column count:"+annotationList.size()); + // Get excel data + List 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); + //判断行是否为空 xuelongjiang@jiadakeji.cn + Iterator cellIterable = row.cellIterator(); + int cellCount = 0; + int cellEmpty = 0; + while (cellIterable.hasNext()){ + Cell itCell = cellIterable.next(); + cellCount++; + if("".equals(itCell.toString())){ + cellEmpty++; + } + } + if(cellCount == cellEmpty){ + continue; + } + StringBuilder sb = new StringBuilder(); + for (Object[] os : annotationList){ + Object val = this.getCellValue(row, column++); + if (val != null){ + ExcelField ef = (ExcelField)os[0]; + // If is dict type, get dict value + if (StringUtils.isNotBlank(ef.dictType())){ + 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+", "); + } + dataList.add(e); + log.debug("Read success: ["+i+"] "+sb.toString()); + } + return dataList; + } + + // /** // * 导入测试 // */ diff --git a/src/main/java/com/thinkgem/jeesite/common/utils/excel/annotation/ExcelField.java b/src/main/java/com/thinkgem/jeesite/common/utils/excel/annotation/ExcelField.java index f07550571689c84ae8253176f9e90a6cf79c58a6..bc65adcd628b960abdafd591bdb80556bef312dc 100644 --- a/src/main/java/com/thinkgem/jeesite/common/utils/excel/annotation/ExcelField.java +++ b/src/main/java/com/thinkgem/jeesite/common/utils/excel/annotation/ExcelField.java @@ -56,4 +56,11 @@ public @interface ExcelField { * 字段归属组(根据分组导出导入) */ int[] groups() default {}; + + /** + * 业务类型,针对每一种业务的导出, + * 根据导出操作时 + * + */ + String bussinessType () default ""; }