一、poi对excel进行读取背景:
在我们日常的开发中经常会有产品经理或者项目上有要求就是实现excel导入这个功能,今天花点时间做了这个功能。在以后工作中可以直接使用和持续完善。
二、实现代码:
1.公用实体-excel组件配置
public class Config {
private int titleRow; //标题行
private int headRow; //头部行
private int startRow; //开始行
private String title; //标题
public int getTitleRow() {
return titleRow;
}
public void setTitleRow(int titleRow) {
this.titleRow = titleRow;
}
public int getHeadRow() {
return headRow;
}
public void setHeadRow(int headRow) {
this.headRow = headRow;
}
public int getStartRow() {
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
2.公用实体-合并单元格结果对象
public class MergeCellResult {
private int firstRow;
private int lastRow;
private int firstColumn;
private int lastColumn;
private boolean isMergeCell;
public MergeCellResult() {
super();
}
public MergeCellResult(int firstRow, int lastRow, int firstColumn, int lastColumn, boolean isMergeCell) {
super();
this.firstRow = firstRow;
this.lastRow = lastRow;
this.firstColumn = firstColumn;
this.lastColumn = lastColumn;
this.isMergeCell = isMergeCell;
}
public int getFirstRow() {
return firstRow;
}
public void setFirstRow(int firstRow) {
this.firstRow = firstRow;
}
public int getLastRow() {
return lastRow;
}
public void setLastRow(int lastRow) {
this.lastRow = lastRow;
}
public int getFirstColumn() {
return firstColumn;
}
public void setFirstColumn(int firstColumn) {
this.firstColumn = firstColumn;
}
public int getLastColumn() {
return lastColumn;
}
public void setLastColumn(int lastColumn) {
this.lastColumn = lastColumn;
}
public boolean isMergeCell() {
return isMergeCell;
}
public void setMergeCell(boolean isMergeCell) {
this.isMergeCell = isMergeCell;
}
}
3.excel组件反射工具类
public class ExcelReflect {
/**
* 从object中获取execel注解字段的值
* @param obj object实体对象
* @param excelFieldMap excel字段集合
* @return 值集合
*/
public static Map<String, String> getFieldsValue(Object obj, Map<String, Object> excelFieldMap) {
Map<String, String> valueMap = new HashedMap<String, String>();
for (String fieldName : excelFieldMap.keySet()) {
AppCloudExcel appCloudExcel = (AppCloudExcel) excelFieldMap.get(fieldName);
valueMap.put(appCloudExcel.serial() + "", fieldExtValue(fieldName, obj).toString());
}
return valueMap;
}
/**
* 获取class类中注解excel的字段集合
* @param clazz class类
* @return excel字段集合
*/
public static <T> Map<String, Object> getClassExcelFieldsList(Class<T> clazz) {
Field[] fields = clazz.getDeclaredFields();
Map<String, Object> excelFieldMap = new HashedMap<String, Object>();
for (int j = 0; j < fields.length; j++) {
AppCloudExcel appCloudExcel = fields[j].getAnnotation(AppCloudExcel.class);
if (null != appCloudExcel) {
excelFieldMap.put(fields[j].getName(), fields[j].getAnnotation(AppCloudExcel.class));
}
}
return excelFieldMap;
}
/**
* list的map集合转换为list的object集合
* @param listMap list的map集合
* @param clazz class类
* @return list的object集合
*/
@SuppressWarnings({
"unchecked" })
public static <T> List<T> listMapToListObj(List<Map<String, String>> listMap, Class<T> clazz) {
List