将StudentInfo.xlsx里的内容读取出来并封装到StudentInfo类的对象中
StudentInfo类的字段分别对应了excel中的列名,合成结果如下
利用反射可以很好地实现对任意类的数据封装。实现如下
public class Test {
public <T> List<T> parseFromExcel(String path, Class<T> aimClass) {
return parseFromExcel(path, 0, aimClass);
}
@SuppressWarnings("deprecation")
public <T> List<T> parseFromExcel(String path, int firstIndex, Class<T> aimClass) {
List<T> result = new ArrayList<T>();
try {
FileInputStream fis = new FileInputStream(path);
Workbook workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheetAt(0);
int lastRaw = sheet.getLastRowNum();
for (int i = firstIndex; i < lastRaw; i++) {
Row row = sheet.getRow(i);
T parseObject = aimClass.newInstance();
Field[] fields = aimClass.getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
Field field = fields[j];
field.setAccessible(true);
Class<?> type = field.getType();
Cell cell = row.getCell(j);
if (cell == null)
continue;
//很重要的一行代码,如果不加,像12345这样的数字是不会给你转成String的,只会给你转成double,而且会导致cell.getStringCellValue()报错
cell.setCellType(Cell.CELL_TYPE_STRING);
String cellContent = cell.getStringCellValue();
cellContent = "".equals(cellContent) ? 0 + "" : cellContent;
if (type.equals(String.class)) {
field.set(parseObject, cellContent);
} else if (type.equals(char.class) || type.equals(Character.class)) {
field.set(parseObject, cellContent.charAt(0));
} else if (type.equals(int.class) || type.equals(Integer.class)) {
field.set(parseObject, Integer.parseInt(cellContent));
} else if (type.equals(long.class) || type.equals(Long.class)) {
field.set(parseObject, Long.parseLong(cellContent));
} else if (type.equals(float.class) || type.equals(Float.class)) {
field.set(parseObject, Float.parseFloat(cellContent));
} else if (type.equals(double.class) || type.equals(Double.class)) {
field.set(parseObject, Double.parseDouble(cellContent));
} else if (type.equals(short.class) || type.equals(Short.class)) {
field.set(parseObject, Short.parseShort(cellContent));
} else if (type.equals(byte.class) || type.equals(Byte.class)) {
field.set(parseObject, Byte.parseByte(cellContent));
} else if (type.equals(boolean.class) || type.equals(Boolean.class)) {
field.set(parseObject, Boolean.parseBoolean(cellContent));
}
}
result.add(parseObject);
}
fis.close();
return result;
} catch (
Exception e) {
e.printStackTrace();
System.err.println("An error occured when parsing object from Excel. at " + this.getClass());
}
return result;
}
public static void main(String[] args) throws Exception {
//参数里的5表示有效行数从第5行开始
List<StudentInfo> studentInfos = new
Test().parseFromExcel("C:\\Users\\unive\\Desktop\\StudentInfo.xlsx", 5,
StudentInfo.class);
for (int i = 0; i < studentInfos.size(); i++) {
System.err.println(studentInfos.get(i).toString());
}
}
}
引用的jar包是Apache POI,版本3.17.x,更新于2017年9月,使用的人数是近年来最多的,Maven坐标如下
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>