From 510f3da7c9f022aff9cd1f8ec5c20e58de3d621c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E9=BE=99=E6=B1=9F?= Date: Fri, 8 Dec 2017 01:11:07 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=20=E5=A2=9E=E5=BC=BA=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=B7=A5=E5=85=B7=20-=20=E5=BD=93=E8=A1=A8?= =?UTF-8?q?=E6=A0=BC=E8=A1=8C=E4=B8=BA=E7=A9=BA=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E5=86=8D=E5=A4=84=E7=90=86=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=A0=BC=20-=20=E5=BD=93=E6=95=B0=E5=AD=97=E4=B8=BA=E7=A7=91?= =?UTF-8?q?=E5=AD=A6=E8=AE=A1=E6=95=B0=E6=AD=A3=E7=A1=AE=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=E6=95=B0=E5=AD=97=20-=20=E5=A4=84=E7=90=86=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E5=AF=BC=E5=87=BA=E4=B8=8D=E6=94=AF=E6=8C=81=E5=90=8C?= =?UTF-8?q?=E4=B8=80=E5=AE=9E=E4=BD=93=E4=B8=8D=E5=90=8C=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E5=AD=97=E6=AE=B5=E5=AF=BC=E5=85=A5=E5=AF=BC?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils/excel/ExportExcel.java | 89 +++++++- .../common/utils/excel/ImportExcel.java | 195 +++++++++++++++++- .../utils/excel/annotation/ExcelField.java | 7 + 3 files changed, 286 insertions(+), 5 deletions(-) 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 bb1ff6d3f..60d3f400c 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 d6a54a95d..6b69a6d9b 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 f07550571..bc65adcd6 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 ""; } -- Gitee