通用解析文件转为对象集合插入
在实际工作中,将Excel文件解析成对象集合然后导入数据库是非常常见的需求,面对不同的对象,我们往往需要实现不同的功能,于是萌生了写一个通用方法,来实现不同类的解析,具体实现思路,通过泛型,注解,加反射来实现
导入依赖
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.3</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.3</version> </dependency>
|
定义注解
1 2 3
| public @interface ExcelColumn { String name(); }
|
具体实现工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| package org.pt;
import org.apache.poi.ss.usermodel.*;
import java.io.File; import java.io.FileInputStream; import java.lang.reflect.Field; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List;
public class ExcelReader<T> { private final Class<T> type;
public ExcelReader(Class<T> type) { this.type = type; }
public List<T> readExcel(String filePath) { List<T> dataList = new ArrayList<>(); try { FileInputStream file = new FileInputStream(new File(filePath)); Workbook workbook = WorkbookFactory.create(file); Sheet sheet = workbook.getSheetAt(0); Row headerRow = sheet.getRow(0); String[] headers = new String[headerRow.getLastCellNum()]; for (int i = 0; i < headerRow.getLastCellNum(); i++) { headers[i] = headerRow.getCell(i).getStringCellValue(); } for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row == null) continue; T obj = type.getDeclaredConstructor().newInstance(); for (int j = 0; j < row.getLastCellNum(); j++) { Cell cell = row.getCell(j); String header = headers[j]; Object value = getCellValue(cell); Field field = getFieldByName(header); if (field != null) { field.setAccessible(true); field.set(obj, convertValueToType(value, type.getTypeName().getClass())); } }
dataList.add(obj); } workbook.close(); file.close(); } catch (Exception e) { e.printStackTrace(); }
return dataList; }
private Field getFieldByName(String name) { for (Field field : type.getDeclaredFields()) { ExcelColumn annotation = field.getAnnotation(ExcelColumn.class); if (annotation != null && annotation.name().equals(name)) { return field; } } return null; }
private Object convertValueToType(Object value, Class<?> type) { if (value == null) { return null; }
if (type == String.class) { return value.toString(); } else if (type == int.class || type == Integer.class) { if (value instanceof String) { return Integer.parseInt((String) value); } else if (value instanceof Number) { return ((Number) value).intValue(); } } else if (type == double.class || type == Double.class) { if (value instanceof String) { return Double.parseDouble((String) value); } else if (value instanceof Number) { return ((Number) value).doubleValue(); } } else if (type == boolean.class || type == Boolean.class) { if (value instanceof String) { return Boolean.parseBoolean((String) value); } else if (value instanceof Boolean) { return value; } } else if (type == Date.class) { if (value instanceof Date) { return value; } else if (value instanceof String) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { return dateFormat.parse((String) value); } catch (ParseException e) { e.printStackTrace(); } } }
return value; }
private Object getCellValue(Cell cell) { if (cell == null) { return null; }
switch (cell.getCellType()) { case STRING: return cell.getStringCellValue(); case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { return cell.getDateCellValue(); } else { return cell.getNumericCellValue(); } case BOOLEAN: return cell.getBooleanCellValue(); case FORMULA: return cell.getCellFormula(); default: return null; } }
public static void main(String[] args) { ExcelReader<Employee> excelReader = new ExcelReader<>(Employee.class); List<Employee> dataList = excelReader.readExcel("your_excel_file.xlsx");
for (Employee employee : dataList) { System.out.println(employee); } }
}
|
结论:用好反射