Package com.thinkgem.jeesite.common.utils.excel.annotation

Examples of com.thinkgem.jeesite.common.utils.excel.annotation.ExcelField


  public <E> List<E> getDataList(Class<E> cls, int... groups) throws InstantiationException, IllegalAccessException{
    List<Object[]> 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 (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 (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<Object[]>() {
      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<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){
        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());
              }
            }
View Full Code Here


   */
  public ExportExcel(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 (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 (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;
              }
View Full Code Here

    for (E e : list){
      int colunm = 0;
      Row row = this.addRow();
      StringBuilder sb = new StringBuilder();
      for (Object[] os : annotationList){
        ExcelField ef = (ExcelField)os[0];
        Object val = null;
        // Get entity value
        try{
          if (StringUtils.isNotBlank(ef.value())){
            val = Reflections.invokeGetter(e, ef.value());
          }else{
            if (os[1] instanceof Field){
              val = Reflections.invokeGetter(e, ((Field)os[1]).getName());
            }else if (os[1] instanceof Method){
              val = Reflections.invokeMethod(e, ((Method)os[1]).getName(), new Class[] {}, new Object[] {});
            }
          }
          // If is dict, get dict label
          if (StringUtils.isNotBlank(ef.dictType())){
            val = DictUtils.getDictLabel(val==null?"":val.toString(), ef.dictType(), "");
          }
        }catch(Exception ex) {
          // Failure to ignore
          log.info(ex.toString());
          val = "";
        }
        this.addCell(row, colunm++, val, ef.align(), ef.fieldType());
        sb.append(val + ", ");
      }
      log.debug("Write success: ["+row.getRowNum()+"] "+sb.toString());
    }
    return this;
View Full Code Here

TOP

Related Classes of com.thinkgem.jeesite.common.utils.excel.annotation.ExcelField

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.