当前位置:网站首页>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;
}

边栏推荐
- Using foreach to loop two-dimensional array
- MySQL第五章总结
- Reasons for "unresolved external symbols" during vs or QT compilation link:
- June training (the 26th day) - collective search
- MySQL第九次作业-连接查询&子查询
- Vscode environment setup: synchronous configuration
- 【在线仿真】Arduino UNO PWM 控制直流电机转速
- MySQL project 8 summary
- Redis中执行Lua脚本
- 【软件项目管理】期末复习知识点整理
猜你喜欢

Cmake / set command

MySQL第十次作业-视图

創建對象的時候堆內存的分配

Little red book - Notes inspiration - project summary

904. fruit baskets

MySQL 11th job - view application

Allocation of heap memory when creating objects

Function run time

Concise course of probability theory and statistics in engineering mathematics second edition review outline

The sixth MySQL job - query data - multiple conditions
随机推荐
2. merge two ordered arrays
SQL Server foundation introduction collation
What is in the method area - class file, class file constant pool, runtime constant pool
Leetcode intermediate node of linked list
MySQL第十二次作业-存储过程的应用
What is LSP
Small example of SSM project, detailed tutorial of SSM integration
Call API interface to generate QR code of wechat applet with different colors
echo $?
用同花顺手机炒股是安全的吗?如何用同花顺炒股
Pit record_ TreeSet custom sorting results in less data loss
MySQL project 7 Summary
June training (the 26th day) - collective search
Yarn package management tool
Global and Chinese market of amateur football helmets 2022-2028: Research Report on technology, participants, trends, market size and share
Opencv image processing - grayscale processing
Search engine advanced search method records
Little red book - Notes inspiration - project summary
How QT uses quazip to compress and decompress files
[depth first search] 312 Poke balloon