当前位置:网站首页>Using reflection to export entity data to excel

Using reflection to export entity data to excel

2022-06-26 10:42:00 MervynLammm

//  Parameter interpretation :
// 1. title, Map<Integer, String[]>
// 1.1 key - Integer,  Number of columns 
// 1.2 value - String[]  Capacity of 2 Array of ,0 Location store entity class attribute name ,1 Location storage excel Column title in 
// 2. data,  Data to be exported 
// 3. clz,  Of the entity class to be exported class
// 4.  file name 
public <T> HSSFWorkbook fillExcel(Map<Integer, String[]> title, List<T> data, Class<T> clz, String fileName) {
    
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet(fileName);
    sheet.setDefaultColumnWidth(15);

    HSSFRow titleRow = sheet.createRow(0);
    for (int i = 0; i < title.size(); ++i) {
    
        titleRow.createCell(i).setCellValue(title.get(i)[1]);
    }

    int dataRow = 1;
    for (int rowNum = 0; rowNum < data.size(); ++rowNum) {
    
        HSSFRow row = sheet.createRow(rowNum + dataRow);
        for (int colNum = 0; colNum < title.size(); ++colNum) {
    
            String propertyName = title.get(colNum)[0];
            Object val = null;
            try {
    
                // Reflection gets property values 
                Field field = clz.getDeclaredField(propertyName);
                // obtain private value 
                field.setAccessible(true);
                val = field.get(data.get(rowNum));
            } catch (Exception e) {
    
                e.printStackTrace();
            }

            if (val == null) {
    
                row.createCell(colNum).setCellValue("");
            } else if (val instanceof String) {
    
                row.createCell(colNum).setCellValue((String) val);
            } else if (val instanceof Integer) {
    
                row.createCell(colNum).setCellValue((Integer) val);
            } else if (val instanceof Double) {
    
                row.createCell(colNum).setCellValue((Double) val);
            } else {
    
                row.createCell(colNum).setCellValue(val + "");
            }
        }
    }
    return workbook;
}

Call example

Entity class

//lombok Omit getter、setter
@Data
public class User {
    
    private String name;
    private Integer age;
    private String city;
    private Integer grade;
    public User() {
    
        
    }
    public User(String name, Integer age, String city, Integer grade) {
    
        this.name = name;
        this.age = age;
        this.city = city;
        this.grade = grade;
    }
}

call

private void exportUser() {
    
    List<User> userList = new ArrayList<>();
    userList.add(new User(" Zhang San ",14," guangzhou ",8));
    userList.add(new User(" Li Si ",18," The Beijing municipal ",12));
    userList.add(new User(" Wang Wu ",9," Shanghai ",3));
    userList.add(new User(" Hao Liu ",16," Wuhan City ",10));
    userList.add(new User(" He Qi ",6," Qingdao ",1));
    
    int index = 0;
    Map<Integer, String[]> title = new HashMap<>();
    title.put(index++, new String[]{
    "name", " full name "});
    title.put(index++, new String[]{
    "age", " Age "});
    title.put(index++, new String[]{
    "city", " city "});
    title.put(index++, new String[]{
    "grade", " grade "});

    HSSFWorkbook wb = this.fillExcel(title, userList, User.class, " User information ");
    this.createExcelFile(wb, "D:\\Download\\", " User information ");
}

public boolean createExcelFile(HSSFWorkbook book, String path, String name) {
    
    boolean flag = true;
    try (FileOutputStream out = new FileOutputStream(path + name + ".xls");) {
    
        book.write(out);
        out.flush();
    } catch (Exception e) {
    
        e.printStackTrace();
        flag = false;
    }
    return flag;
}

image-20210406141632816

原网站

版权声明
本文为[MervynLammm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170529155746.html