导入导出工具类自定义数据格式化改为由两个方法分别执行导入导出时的数据格式转换

pull/350/head
张岩 2 years ago
parent 2eaabefeac
commit e492538513

@ -8,17 +8,49 @@ import org.apache.poi.ss.usermodel.Workbook;
* *
* @author ruoyi * @author ruoyi
*/ */
public interface ExcelHandlerAdapter @SuppressWarnings("unused")
{ public interface ExcelHandlerAdapter {
/** /**
* *
* *
* @deprecated 使
*
* @param value * @param value
* @param args excelargs * @param args excelargs
* @param cell * @param cell
* @param wb 簿 * @param wb 簿
* @return
*/
@Deprecated
default Object format(Object value, String[] args, Cell cell, Workbook wb) {
return value;
}
/**
*
* *
* @param value
* @param args excelargs
* @param cell
* @param wb 簿
* @return * @return
*/ */
Object format(Object value, String[] args, Cell cell, Workbook wb); default Object importFormat(Object value, String[] args, Cell cell, Workbook wb) {
return value;
}
/**
*
*
* @param value
* @param args excelargs
* @param cell
* @param wb 簿
* @return
*/
default Object exportFormat(Object value, String[] args, Cell cell, Workbook wb) {
return value;
}
} }

@ -434,7 +434,7 @@ public class ExcelUtil<T>
} }
else if (!attr.handler().equals(ExcelHandlerAdapter.class)) else if (!attr.handler().equals(ExcelHandlerAdapter.class))
{ {
val = dataFormatHandlerAdapter(val, attr, null); val = importFormatHandlerAdapter(val, attr, null);
} }
ReflectUtils.invokeSetter(entity, propertyName, val); ReflectUtils.invokeSetter(entity, propertyName, val);
} }
@ -924,7 +924,7 @@ public class ExcelUtil<T>
} }
else if (!attr.handler().equals(ExcelHandlerAdapter.class)) else if (!attr.handler().equals(ExcelHandlerAdapter.class))
{ {
cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell)); cell.setCellValue(exportFormatHandlerAdapter(value, attr, cell));
} }
else else
{ {
@ -1107,10 +1107,13 @@ public class ExcelUtil<T>
/** /**
* *
* *
* @deprecated 使
*
* @param value * @param value
* @param excel * @param excel
* @return * @return
*/ */
@Deprecated
public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell) public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell)
{ {
try try
@ -1126,6 +1129,46 @@ public class ExcelUtil<T>
return Convert.toStr(value); return Convert.toStr(value);
} }
/**
*
*
* @param value
* @param excel
* @param cell
* @return
*/
public String importFormatHandlerAdapter(Object value, Excel excel, Cell cell) {
try {
Object instance = excel.handler().getDeclaredConstructor().newInstance();
Method formatMethod = excel.handler().getMethod("importFormat",
Object.class, String[].class, Cell.class, Workbook.class);
value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb);
} catch (Exception e) {
log.error("不能格式化数据 " + excel.handler() + "{}", e.getMessage());
}
return Convert.toStr(value);
}
/**
*
*
* @param value
* @param excel
* @param cell
* @return
*/
public String exportFormatHandlerAdapter(Object value, Excel excel, Cell cell) {
try {
Object instance = excel.handler().getDeclaredConstructor().newInstance();
Method formatMethod = excel.handler().getMethod("exportFormat",
Object.class, String[].class, Cell.class, Workbook.class);
value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb);
} catch (Exception e) {
log.error("不能格式化数据 " + excel.handler() + "{}", e.getMessage());
}
return Convert.toStr(value);
}
/** /**
* *
*/ */

Loading…
Cancel
Save