public boolean doesClassHaveExcelReaderAnnotation(Class<?> clazz) {
   ExcelReader excelReader = clazz.getAnnotation(ExcelReader.class);
   if (excelReader == null) {
     return false;
   }
   return true;
 }
  public int parse(Class<?> clazz) throws Exception {

    if (!doesClassHaveExcelReaderAnnotation(clazz)) {
      return 0;
    }

    String fileName = getFileName(clazz);

    Sheet sheet = getSheet(fileName);

    Field[] fields = clazz.getDeclaredFields();
    int count = 0;

    for (Field field : fields) {
      if (field.isAnnotationPresent(TestCase.class)) {
        try {
          // method.invoke(null);
          System.out.println("Field name: " + field.getName());
          count++;
        } catch (Exception e) {
        }
      }
    }

    return count;
  }
  public void parseData(
      final Sheet sheet,
      final int startRow,
      final int endColumn,
      final List<String> propertyNames,
      final Class clazzOfTestCase) {

    int rowCounter = startRow;

    while (!isBlank(sheet, rowCounter, endColumn)) {
      Row row = sheet.getRow(rowCounter - 1);
      for (int i = 0; i < endColumn; i++) {
        Cell cell = row.getCell(i);
        if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
          try {
            Method method = clazzOfTestCase.getMethod("set" + propertyNames.get(i), Object.class);
          } catch (NoSuchMethodException e) {
            e
                .printStackTrace(); // To change body of catch statement use File | Settings | File
                                    // Templates.
          }
        }
      }
    }
  }
  public String getFileName(Class<?> clazz) {
    ExcelReader excelReader = clazz.getAnnotation(ExcelReader.class);
    if (excelReader == null || excelReader.fileName().trim().length() == 0) {
      return "";
    }

    return excelReader.fileName();
  }
Ejemplo n.º 5
0
 private List<ExcelHeader> getHeaderList(Class clz) {
   List<ExcelHeader> headers = new ArrayList<ExcelHeader>();
   // 获取全部get/is方法
   Method[] ms = clz.getDeclaredMethods();
   for (Method m : ms) {
     String mn = m.getName();
     if (mn.startsWith("get") || mn.startsWith("is")) {
       if (m.isAnnotationPresent(ExcelResources.class)) {
         ExcelResources er = m.getAnnotation(ExcelResources.class);
         headers.add(new ExcelHeader(er.title(), er.order(), mn));
       }
     }
   }
   return headers;
 }
Ejemplo n.º 6
0
 public List<Object> readExcel(Workbook wb, Class clz, int readLine, int tailLine) {
   Sheet sheet = wb.getSheetAt(0); // 取第一张表
   List<Object> objs = null;
   try {
     Row row = sheet.getRow(readLine); // 开始行,主题栏
     objs = new ArrayList<Object>();
     Map<Integer, String> maps = getHeaderMap(row, clz); // 设定对应的字段顺序与方法名
     if (maps == null || maps.size() <= 0)
       throw new RuntimeException("要读取的Excel的格式不正确,检查是否设定了合适的行"); // 与order顺序不符
     for (int i = readLine + 1; i <= sheet.getLastRowNum() - tailLine; i++) { // 取数据
       row = sheet.getRow(i);
       Object obj = clz.newInstance(); //   调用无参结构
       for (Cell c : row) {
         int ci = c.getColumnIndex();
         String mn = maps.get(ci).substring(3); // 消除get
         mn = mn.substring(0, 1).toLowerCase() + mn.substring(1);
         Map<String, Object> params = new HashMap<String, Object>();
         if (!"enterDate".equals(mn)) c.setCellType(Cell.CELL_TYPE_STRING); // 设置单元格格式
         else c.setCellType(Cell.CELL_TYPE_NUMERIC);
         if (this.getCellValue(c).trim().equals("是")) {
           BeanUtils.copyProperty(obj, mn, 1);
         } else if (this.getCellValue(c).trim().equals("否")) {
           BeanUtils.copyProperty(obj, mn, 0);
         } else BeanUtils.copyProperty(obj, mn, this.getCellValue(c));
       }
       objs.add(obj);
     }
   } catch (InstantiationException e) {
     e.printStackTrace();
     logger.error(e);
   } catch (IllegalAccessException e) {
     e.printStackTrace();
     logger.error(e);
   } catch (InvocationTargetException e) {
     e.printStackTrace();
     logger.error(e);
   } catch (NumberFormatException e) {
     e.printStackTrace();
     logger.error(e);
   }
   return objs;
 }