我是靠谱客的博主 危机大地,最近开发中收集的这篇文章主要介绍java poi通过反射进行批量导入工具类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 

/**
*
* excel数据 读取
*
* @author
* @version [VCES V201R001, 2017年12月11日]
*
* @param customClass
实体类
* @param workbook
excel
* @param indexSheet
sheet 下标
* @param startingLine
数据起始行
* @param attr
实体类对应字段名称数组,
* @return
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InstantiationException
*/
public static <T> List<T> excelImportBoty(Class<T> customClass, HSSFWorkbook workbook,
Integer indexSheet, Integer startingLine, String[] attr) throws Exception {
List<T> result = new ArrayList<T>();
// 获取sheet
HSSFSheet sheetAt = workbook.getSheetAt(indexSheet);
// 获取总行数
int lastRowNum = sheetAt.getLastRowNum();
HSSFRow createRow = null;
Field commandField;
// 开始循环读取数据 ,从数据其实行开始
for (int i = startingLine; i < lastRowNum + 1; i++) {
createRow = sheetAt.getRow(i);
T ob = customClass.newInstance();// 创建实例化对象,创建新的存储数据的对象
// 遍历所有属性值
for (int j = 0; j < attr.length; j++) {
commandField = customClass.getDeclaredField(attr[j]);
// 开启属性访问权限
commandField.setAccessible(true);
// 获取数据
String data = createRow.getCell(j).toString();
// 判断属性的类型
if (commandField.getType().toString().equals("class java.lang.String")) {
// 将读入的数据(在con中)封装到对象(ob)的属性(fi[i])中

commandField.set(ob, data);
} else if (commandField.getType().toString().equals("class java.math.BigDecimal")) {
commandField.set(ob, new BigDecimal(data));//

} else if (commandField.getType().toString().equals("class java.math.BigInteger")) {
commandField.set(ob, new BigInteger(data));// 将信息封装到对象中
} else if (commandField.getType().toString().equals("class java.lang.Integer")) {
commandField.set(ob, Integer.valueOf(data));// 将信息封装到对象中

}
}
result.add(ob);
}
return result;
}


 

转载于:https://www.cnblogs.com/cjbbk/p/8057300.html

最后

以上就是危机大地为你收集整理的java poi通过反射进行批量导入工具类的全部内容,希望文章能够帮你解决java poi通过反射进行批量导入工具类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部