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

边栏推荐
- 【Leetcode】76. Minimum covering substring
- MySQL第四章总结
- 3行3列整形二维数组,求对角之和
- Little red book - Summary of internal sales spike project
- What are the symbolic and direct references of the JVM
- OpenCV图像处理-灰度处理
- Express (I) - easy to get started
- 36 qtextedit control input multiline text
- Installer MySQL sous Linux [détails]
- 2. merge two ordered arrays
猜你喜欢

基础-MySQL

Hcia-dhcp experiment

Vscode environment setup: synchronous configuration

Adaptiveavgpool2d does not support onnx export. Customize a class to replace adaptiveavgpool2d

AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D

Reshape a two-dimensional array with 3 rows and 3 columns to find the sum of the diagonals

Which PHP open source works deserve attention

MySQL第六次作业-查询数据-多条件

Record the handling of oom problems caused by too many threads at one time

MySQL 10th job - View
随机推荐
Reshape a two-dimensional array with 3 rows and 3 columns to find the sum of the diagonals
[difficult and miscellaneous diseases] @transitional failure summary
Flutter and native communication (Part 1)
MySQL第十四次作业--电子商城项目
Linux下安裝Mysql【詳細】
MySQL第八次作业
MySQL 12th job - Application of stored procedure
小笔记-简单但够用系列_KVM快速入门
Global and Chinese market of aluminum sunshade systems 2022-2028: Research Report on technology, participants, trends, market size and share
Oracle11g reports an error when starting the database ora-27154: post/wait create failed
基础-MySQL
When will JVM garbage collection enter the older generation
Oracle11g 启动数据库时报错 ORA-27154: post/wait create failed
搜索引擎高级搜索方法记录
How to change the QR code material color of wechat applet
Redis中执行Lua脚本
Searchview click failure
Idea remote debugger
2. merge two ordered arrays
Allocation of heap memory when creating objects