コード例 #1
0
  /** 获得实体类和Excel的对应关系 */
  @Override
  public Map<String, FinanceReportBean> getClassFieldExcel(String targetTable, String className)
      throws Exception {
    Map<String, FinanceReportBean> FinanceField = new HashMap<String, FinanceReportBean>();
    String tablename = "";
    String sfieldIndex = "";
    String sfieldName = "";
    String sfield = "";
    if (null == className || "".equals(className) || "null".equalsIgnoreCase(className)) {
      throw new BusinessException("没有给定要导的实体类");
    } else {
      try {
        Class<?> demo1 = null;
        demo1 = Class.forName(className);
        FieldName ctableField = (FieldName) demo1.getAnnotation(FieldName.class);
        if (null == ctableField) {
          throw new BusinessException("类" + className + "没有加@FieldName注解");
        } else {
          tablename = ctableField.name();
          if ("".equals(tablename)) {
            throw new BusinessException("类" + className + "类表上没@FieldName有加注解");
          }
          if (!targetTable.equalsIgnoreCase(tablename)) {
            throw new BusinessException(
                "要导入的实体类" + targetTable + "和类上@FieldName有加注解" + tablename + "不一样");
          }
        }
        Field[] field = demo1.getDeclaredFields();
        for (int i = 0; i < field.length; i++) {
          sfieldIndex = "";
          sfieldName = "";
          sfield = "";
          Field fd = field[i];
          FieldName anF = (FieldName) fd.getAnnotation(FieldName.class);
          FieldIndex index = (FieldIndex) fd.getAnnotation(FieldIndex.class);
          if (null != index) {
            if (!"".equals(index.value())) {
              sfieldIndex = index.value();
              sfield = fd.getName().toLowerCase().toString().trim();
              if (null == anF || "".equals(anF.name().toString().trim())) {
                throw new BusinessException("类" + tablename + "的" + sfield + "没有加@FieldName注解");
              } else {
                sfieldName = anF.name().toString().trim();
              }

              FinanceField.put(sfieldIndex, new FinanceReportBean(sfieldIndex, sfieldName, sfield));
            }
          }
        }
      } catch (Exception e) {
        // TODO Auto-generated catch block
        throw new BusinessException("getClassFieldExcel在解析实体类时出错" + e.getMessage());
      }
    }
    return FinanceField;
  }