溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java利用注解實現(xiàn)簡單的excel數(shù)據(jù)讀取

發(fā)布時間:2020-09-26 11:52:17 來源:腳本之家 閱讀:140 作者:aheizi 欄目:編程語言

實現(xiàn)工具類

利用注解實現(xiàn)簡單的excel數(shù)據(jù)讀取,利用注解對類的屬性和excel中的表頭映射,使用Apache的poi就不用在業(yè)務代碼中涉及row,rows這些屬性了。

定義注解:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Excel {
 
 String name();

}

由于本例中只涉及根據(jù)Excel表頭部分對Excel進行解析,只定義了一個name作為和Excel表頭的隱射。

工具類完整代碼如下:

public class ExcelUtil<T> {

 Class<T> clazz;

 public ExcelUtil(Class<T> clazz) {
  this.clazz = clazz;
 }

 public List<T> importExcel(String sheetName, InputStream input) {
  int maxCol = 0;
  List<T> list = new ArrayList<T>();
  try {
   Workbook workbook = WorkbookFactory.create(input);
   Sheet sheet = workbook.getSheet(sheetName);
   // 如果指定sheet名,則取指定sheet中的內容.
   if (!sheetName.trim().equals("")) {
    sheet = workbook.getSheet(sheetName);
   }
   // 如果傳入的sheet名不存在則默認指向第1個sheet.
   if (sheet == null) {
    sheet = workbook.getSheetAt(0);
   }
   int rows = sheet.getPhysicalNumberOfRows();
   // 有數(shù)據(jù)時才處理
   if (rows > 0) {
    List<Field> allFields = getMappedFiled(clazz, null);
    // 定義一個map用于存放列的序號和field.
    Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>();
    // 第一行為表頭
    Row rowHead = sheet.getRow(0);
    Map<String, Integer> cellMap = new HashMap<>();
    int cellNum = rowHead.getPhysicalNumberOfCells();
    for (int i = 0; i < cellNum; i++){
     cellMap.put(rowHead.getCell(i).getStringCellValue().toLowerCase(), i);
    }
    for (Field field : allFields) {
     // 將有注解的field存放到map中.
     if (field.isAnnotationPresent(Excel.class)) {
      Excel attr = field.getAnnotation(Excel.class);
      // 根據(jù)Name來獲取相應的failed
      int col = cellMap.get(attr.name().toLowerCase());
      field.setAccessible(true);
      fieldsMap.put(col, field);
     }
    }
    // 從第2行開始取數(shù)據(jù)
    for (int i = 1; i < rows; i++) {
     Row row = sheet.getRow(i);
     T entity = null;
     for (int j = 0; j < cellNum; j++) {
      Cell cell = row.getCell(j);
      if (cell == null) {
       continue;
      }
      int cellType = cell.getCellType();
      String c = "";
      if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
       DecimalFormat df = new DecimalFormat("0");
       c = df.format(cell.getNumericCellValue());
      } else if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
       c = String.valueOf(cell.getBooleanCellValue());
      } else {
       c = cell.getStringCellValue();
      }
      if (c == null || c.equals("")) {
       continue;
      }
      entity = (entity == null ? clazz.newInstance() : entity);
      // 從map中得到對應列的field.
      Field field = fieldsMap.get(j);
      if (field == null) {
       continue;
      }
      // 取得類型,并根據(jù)對象類型設置值.
      Class<?> fieldType = field.getType();
      if (String.class == fieldType) {
       field.set(entity, String.valueOf(c));
      } else if ((Integer.TYPE == fieldType)
        || (Integer.class == fieldType)) {
       field.set(entity, Integer.valueOf(c));
      } else if ((Long.TYPE == fieldType)
        || (Long.class == fieldType)) {
       field.set(entity, Long.valueOf(c));
      } else if ((Float.TYPE == fieldType)
        || (Float.class == fieldType)) {
       field.set(entity, Float.valueOf(c));
      } else if ((Short.TYPE == fieldType)
        || (Short.class == fieldType)) {
       field.set(entity, Short.valueOf(c));
      } else if ((Double.TYPE == fieldType)
        || (Double.class == fieldType)) {
       field.set(entity, Double.valueOf(c));
      } else if (Character.TYPE == fieldType) {
       if (c.length() > 0) {
        field.set(entity, c.charAt(0));
       }
      }
     }
     if (entity != null) {
      list.add(entity);
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return list;
 }


 /**
  * 得到實體類所有通過注解映射了數(shù)據(jù)表的字段
  *
  * @param clazz
  * @param fields
  * @return
  */
 private List<Field> getMappedFiled(Class clazz, List<Field> fields) {
  if (fields == null) {
   fields = new ArrayList<Field>();
  }
  // 得到所有定義字段
  Field[] allFields = clazz.getDeclaredFields();
  // 得到所有field并存放到一個list中.
  for (Field field : allFields) {
   if (field.isAnnotationPresent(Excel.class)) {
    fields.add(field);
   }
  }
  if (clazz.getSuperclass() != null
    && !clazz.getSuperclass().equals(Object.class)) {
   getMappedFiled(clazz.getSuperclass(), fields);
  }

  return fields;
 }


}

代碼很簡單,獲取sheet,解析第一行,并和實體類標有注解的字段一一對應,用hashMap記錄下來,然后循環(huán)取得Excel中剩下所有的數(shù)據(jù),根據(jù)map的對應關系將值set到對應字段。

基本使用

待解析表格如下:

java利用注解實現(xiàn)簡單的excel數(shù)據(jù)讀取

定義實體類:

public class User {

 @Excel(name = "filed1")
 private String name;
 @Excel(name = "filed2")
 private String nameEn;
 @Excel(name = "filed3")
 private Integer age;
 @Excel(name = "filed4")
 private String six;
 @Excel(name = "filed5")
 private String weight;

 // ...getter setter
}

使用工具類:

public static void main (String[] args) {
 FileInputStream fileInputStream = null;
 try {
  fileInputStream = new FileInputStream("D://data.xlsx");
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }
 ExcelUtil<User> util = new ExcelUtil<>(User.class);
 List<User> jalanHotelList = util.importExcel("user", fileInputStream);
 // do something
}

利用這個思路可以擴展出導出excel功能,利用注解指定導出的excel表頭,甚至可以輕松控制excel表頭的顏色,合并屬性等等,在xdemo中有詳細復雜的示例,可以研究下。由于我的需求很簡單,就不整那么復雜啦。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI