poi版本为3.14
公共的导入方法:
/**
* 公共导入的方法,根据传入的map对应的实体类的数据类型取值
* 可以分辨的数据类型为:String、double、Double、long、Long、int、Integer、Timestamp
* @param map 需要导入的字段的map集合,key为excel的表头,value为实体类的熟悉,如:map.put("名字", "name");
* @param vo1 实体类
* @param path 文件路径
* @return
* @author pc
* @Date
*/
public Object excelOutPut(Map<String, String> map,Class<?> vo1,String path){
List<Object> list = new ArrayList<Object>();
try {
FileInputStream fis = new FileInputStream(path);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HSSFWorkbook workbook = new HSSFWorkbook(fs);//创建工作簿
HSSFSheet sheet = workbook.getSheetAt(0);//获取第1个工作表
int rows = sheet.getPhysicalNumberOfRows();//获取行数
int cellNumber = sheet.getRow(0).getPhysicalNumberOfCells();//获取第一行的列数
Field[] fields = vo1.getDeclaredFields();//利用反射,返回 Field 对象的一个数组,这些对象反映此 Class 对象所表示的类或接口所声明的所有字段。
for(int i=1;i<rows;i++){//循环Excel文件的每一行
HSSFRow row = sheet.getRow(i);//获取第i行
Object vo = vo1.getConstructor(new Class[]{}).newInstance(new Object[]{});//通过反射创建对象
for(int j=0; j<cellNumber; j++){
Cell cell = row.getCell(j);//获取单元格
if (null == cell) {
cell = row.createCell(j);
}
String headname = sheet.getRow(0).getCell(j).getStringCellValue();//获取第一个行的头
String file_name = map.get(headname);//根据头,在传入的map中获取其对应的value
if(null == file_name){//表示map中没有这个,不需要这个值。
continue;
}
String fieldName = "" ;//需要的实体类的属性名字
String fieldType = "" ;//需要的实体类的属性的数据类型
for(int c=0;c<fields.length;c++){
String fieldName1 = fields[c].getName();
if(fieldName1.equals(file_name)){
fieldName = fieldName1 ;
fieldType = fields[c].getGenericType().toString();
break;
}
}
String methodName = "set" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
Method setMethod = null;
if(fieldType.equals("class java.lang.String")){
cell.setCellType(1);//给单元格设置数据格式,为字符串
String value = cell.getStringCellValue();
setMethod = vo.getClass().getMethod(methodName, new Class[]{String.class});
setMethod.invoke(vo, new Object[]{value});
}else if(fieldType.equals("double")){
cell.setCellType(1);
String value = cell.getStringCellValue();
setMethod = vo.getClass().getMethod(methodName,new Class[]{double.class});
setMethod.invoke(vo, Double.parseDouble(value));
}else if(fieldType.equals("class java.lang.Double") ){
cell.setCellType(1);
String value = cell.getStringCellValue();
setMethod = vo.getClass().getMethod(methodName,new Class[]{Double.class});
setMethod.invoke(vo, Double.parseDouble(value));
}else if(fieldType.equals("long")){
cell.setCellType(1);
String value = cell.getStringCellValue();
setMethod = vo.getClass().getDeclaredMethod(methodName,new Class[]{long.class});
setMethod.invoke(vo, Long.parseLong(value));
}else if( fieldType.equals("class java.lang.Long") ){
cell.setCellType(1);
String value = cell.getStringCellValue();
setMethod = vo.getClass().getDeclaredMethod(methodName,new Class[]{Long.class});
setMethod.invoke(vo, Long.parseLong(value));
}else if(fieldType.equals("int")){
cell.setCellType(1);
String value = cell.getStringCellValue();
setMethod = vo.getClass().getMethod(methodName,new Class[]{int.class});
setMethod.invoke(vo, Integer.parseInt(value));
}else if( fieldType.equals("class java.lang.Integer")){
cell.setCellType(1);
String value = cell.getStringCellValue();
setMethod = vo.getClass().getMethod(methodName,new Class[]{Integer.class});
setMethod.invoke(vo, Integer.parseInt(value));
}else if(fieldType.equals("class java.sql.Timestamp")){
Date value = cell.getDateCellValue();
setMethod = vo.getClass().getMethod(methodName,new Class[]{Timestamp.class});
setMethod.invoke(vo, new Timestamp(value.getTime()));
}
}
list.add(vo);
}
fis.close();
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
main函数测试:
public static void main(String[] args){
Map<String, String> map = new HashMap<>();
map.put("名字", "name");
map.put("密码", "password");
map.put("手机号码", "phone");
map.put("时间", "date");
map.put("时间", "date");
map.put("时间2", "dateTiem");
map.put("小数", "dou");
test t = new test();
String path = "D://3.xls";
List<UserBo> list = (List<UserBo>) t.excelOutPut(map,UserBo.class,path);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getDateTiem() +","+ list.get(i).getDate());
}
}
实体类(get和set方法在这里省略):
public class UserBo {
private Integer id;
private String name;
private String password;//密码
private String phone;//手机号码
private Timestamp date;//时间
private String dateTiem;//时间2
private boolean bo;//布尔
private Double dou;//小数
private long lo;//长度
private Long lon;//二长度
private double doub;//二小数
}
导入的excel表格:
测试数据的结果:
42006,2015-01-02 00:00:00.0
42007,2015-01-03 00:00:00.0
42008,2015-01-04 00:00:00.0
42009,2015-01-05 00:00:00.0
42010,2015-01-06 00:00:00.0
42011,2015-01-07 00:00:00.0
42012,2015-01-08 00:00:00.0
42013,2015-01-09 00:00:00.0
这个公共方法,不能导入布尔类型的数据。
该方法用到的知识有:POI导入、反射、Class类、Object类。
反射用到的类是:Field类、Method类
实现该方法的流程为:
1、传入map集合和实体类。
2、由于实体类不固定,所有用class类来接收。
3、通过class类的getDeclaredFields()方法,可以得到传入的实体类所表示的类或接口所声明的所有字段,返回的是反射类的Field[]。
4、通过反射,可以把class类重新创建成一个对象
5、通过poi获取第一行的头,根据这个头去map集合取出其对应的属性
6、通过从map集合中取出的属性和Field[]中的方法名字比较,获取其属性名和属性的类型
7、通过属性类型和poi给不同的单元格设置不同的数据类型,并取值
8、通过反射中的Method类,给创建的对象赋值。
9、把对象添加到List集合中
10、返回对象