利用java反射机制实现读取excel表格中的数据

时间:2023-03-09 08:06:31

如果直接把excel表格中的数据导入数据库,首先应该将excel中的数据读取出来。

为了实现代码重用,所以使用了Object,而最终的结果是要获取一个list如List<User>、List<Book>等,所以需要使用泛型机制去实现。下面会给出代码,可能会稍微复杂一点,但注释很清晰,希望大家耐心阅读。

在上代码之前简单说一下思路:

1.excel表格必须有表头,且表头中各列的值要与实体类的属性相同;

2.先读取表头信息,然后获取表头列数,接着确定需要使用的set方法的名称,并存到数组中;

3.利用反射机制,获取object对象的属性,通过与excel表格表头对比,确定各个属性的类型,存到数组中(以excel表格中属性的顺序);

4.遍历除表头行的数据,利用反射机制实例化对象,调用对应的set方法,方法参数通过3获取;

上代码:(需要使用poi包,请自行下载)

实体类User:

public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }

处理类:

package module.system.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; /**
* 从excel读取数据/往excel中写入 excel有表头,表头每列的内容对应实体类的属性
*
* @author nagsh
*
*/
public class ExcelManage {
private HSSFWorkbook workbook; public ExcelManage(String fileDir) {
File file = new File(fileDir);
try {
workbook = new HSSFWorkbook(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 读取excel表中的数据.
*
* @param sheetName
* 表格索引(EXCEL 是多表文档,所以需要输入表索引号,如sheet1)
*/
public List readFromExcel(String sheetName, Object object) { List result = new ArrayList();
// 获取该对象的class对象
Class class_ = object.getClass();
// 获得该类的所有属性
Field[] fields = class_.getDeclaredFields(); // 读取excel数据
// 获得指定的excel表
HSSFSheet sheet = workbook.getSheet(sheetName);
// 获取表格的总行数
int rowCount = sheet.getLastRowNum() + 1; // 需要加一
if (rowCount < 1) {
return result;
}
// 获取表头的列数
int columnCount = sheet.getRow(0).getLastCellNum();
// 读取表头信息,确定需要用的方法名---set方法
// 用于存储方法名
String[] methodNames = new String[columnCount]; // 表头列数即为需要的set方法个数
// 用于存储属性类型
String[] fieldTypes = new String[columnCount];
// 获得表头行对象
HSSFRow titleRow = sheet.getRow(0);
// 遍历
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { // 遍历表头列
String data = titleRow.getCell(columnIndex).toString(); // 某一列的内容
String Udata = Character.toUpperCase(data.charAt(0))
+ data.substring(1, data.length()); // 使其首字母大写
methodNames[columnIndex] = "set" + Udata;
for (int i = 0; i < fields.length; i++) { // 遍历属性数组
if (data.equals(fields[i].getName())) { // 属性与表头相等
fieldTypes[columnIndex] = fields[i].getType().getName(); // 将属性类型放到数组中
}
}
}
// 逐行读取数据 从1开始 忽略表头
for (int rowIndex = 1; rowIndex < rowCount; rowIndex++) {
// 获得行对象
HSSFRow row = sheet.getRow(rowIndex);
if (row != null) {
Object obj = null;
// 实例化该泛型类的对象一个对象
try {
obj = class_.newInstance();
} catch (Exception e1) {
e1.printStackTrace();
} // 获得本行中各单元格中的数据
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
String data = row.getCell(columnIndex).toString();
// 获取要调用方法的方法名
String methodName = methodNames[columnIndex];
Method method = null;
try {
// 这部分可自己扩展
if (fieldTypes[columnIndex].equals("java.lang.String")) {
method = class_.getDeclaredMethod(methodName,
String.class); // 设置要执行的方法--set方法参数为String
method.invoke(obj, data); // 执行该方法
} else if (fieldTypes[columnIndex].equals("int")) {
method = class_.getDeclaredMethod(methodName,
int.class); // 设置要执行的方法--set方法参数为int
double data_double = Double.parseDouble(data);
int data_int = (int) data_double;
method.invoke(obj, data_int); // 执行该方法
}
} catch (Exception e) {
e.printStackTrace();
}
}
result.add(obj);
}
}
return result;
} public static void main(String[] args) {
ExcelManage em = new ExcelManage("E:/test.xls");
User user = new User();
List list = em.readFromExcel("sheet1", user);
for (int i = 0; i < list.size(); i++) {
User newUser = (User) list.get(i);
System.out.println(newUser.getId() + " " + newUser.getName() + " "
+ newUser.getPassword());
} } }

excel表格:

利用java反射机制实现读取excel表格中的数据

运行结果:

1 aa qqqq
2 bb wwwww
3 cc eeee
4 dd rrrr
5 ee tttt

版权声明:本文为博主原创文章,未经博主允许不得转载。