|
|
@@ -5,6 +5,7 @@ import com.global.base.log.LogPrintMgr;
|
|
|
import com.shkpr.service.customgateway.core.annotation.ExcelMapping;
|
|
|
import com.shkpr.service.customgateway.core.constants.ExcelEnum;
|
|
|
import com.shkpr.service.customgateway.core.constants.ExcelMetadata;
|
|
|
+import com.shkpr.service.customgateway.core.constants.ExcelType;
|
|
|
import com.shkpr.service.customgateway.core.constants.LogFlagBusiType;
|
|
|
import org.apache.commons.collections4.map.CaseInsensitiveMap;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
@@ -22,6 +23,7 @@ import java.lang.reflect.InvocationTargetException;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Paths;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -52,16 +54,16 @@ public class ExcelUtil {
|
|
|
*/
|
|
|
public static Map<String, List<Map<String, String>>> parseExcel(String path) throws InterruptedException {
|
|
|
//excel枚举
|
|
|
- ExcelEnum excelEnum = null;
|
|
|
+ ExcelType excelType = null;
|
|
|
//文件后缀
|
|
|
String ext = path.substring(path.lastIndexOf("."));
|
|
|
//根据后缀名指定枚举
|
|
|
- if (".xls".equalsIgnoreCase(ext)) excelEnum = ExcelEnum.XLS;
|
|
|
- else if (".xlsx".equalsIgnoreCase(ext)) excelEnum = ExcelEnum.XLS;
|
|
|
- if (excelEnum == null) return null;
|
|
|
+ if (".xls".equalsIgnoreCase(ext)) excelType = ExcelType.XLS;
|
|
|
+ else if (".xlsx".equalsIgnoreCase(ext)) excelType = ExcelType.XLS;
|
|
|
+ if (excelType == null) return null;
|
|
|
//解析文件
|
|
|
try {
|
|
|
- return parseExcel(Files.newInputStream(Paths.get(path)), excelEnum);
|
|
|
+ return parseExcel(Files.newInputStream(Paths.get(path)), excelType);
|
|
|
} catch (IOException e) {
|
|
|
LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, BIZ_TYPE, CLASS_NAME
|
|
|
, String.format("excel文件解释失败 msg:%s", e.getMessage())
|
|
|
@@ -74,13 +76,13 @@ public class ExcelUtil {
|
|
|
* 解析excel
|
|
|
*
|
|
|
* @param inputStream 输入流
|
|
|
- * @param excelEnum excel类型
|
|
|
+ * @param excelType excel类型
|
|
|
* @return 数据(key : 页名, v : 数据)
|
|
|
*/
|
|
|
- public static Map<String, List<Map<String, String>>> parseExcel(InputStream inputStream, ExcelEnum excelEnum) throws InterruptedException {
|
|
|
- if (inputStream == null || excelEnum == null || excelEnum == ExcelEnum.CSV) return null;
|
|
|
+ public static Map<String, List<Map<String, String>>> parseExcel(InputStream inputStream, ExcelType excelType) throws InterruptedException {
|
|
|
+ if (inputStream == null || excelType == null || excelType == ExcelType.CSV) return null;
|
|
|
//读取输入流
|
|
|
- try (Workbook workbook = excelEnum == ExcelEnum.XLSX ? new XSSFWorkbook(inputStream) : new HSSFWorkbook(inputStream)) {
|
|
|
+ try (Workbook workbook = excelType == ExcelType.XLSX ? new XSSFWorkbook(inputStream) : new HSSFWorkbook(inputStream)) {
|
|
|
if (workbook.getNumberOfSheets() <= 0) return null;
|
|
|
//结果
|
|
|
Map<String, List<Map<String, String>>> results = new HashMap<>(workbook.getNumberOfSheets());
|
|
|
@@ -157,16 +159,16 @@ public class ExcelUtil {
|
|
|
*/
|
|
|
public static <E> Map<String, List<E>> parseExcel(String path, Class<E> clazz) {
|
|
|
//excel枚举
|
|
|
- ExcelEnum excelEnum = null;
|
|
|
+ ExcelType excelType = null;
|
|
|
//文件后缀
|
|
|
String ext = path.substring(path.lastIndexOf("."));
|
|
|
//根据后缀名指定枚举
|
|
|
- if (".xls".equalsIgnoreCase(ext)) excelEnum = ExcelEnum.XLS;
|
|
|
- else if (".xlsx".equalsIgnoreCase(ext)) excelEnum = ExcelEnum.XLSX;
|
|
|
- if (excelEnum == null) return null;
|
|
|
+ if (".xls".equalsIgnoreCase(ext)) excelType = ExcelType.XLS;
|
|
|
+ else if (".xlsx".equalsIgnoreCase(ext)) excelType = ExcelType.XLSX;
|
|
|
+ if (excelType == null) return null;
|
|
|
//解析文件
|
|
|
try {
|
|
|
- return parseExcel(Files.newInputStream(Paths.get(path)), clazz, excelEnum);
|
|
|
+ return parseExcel(Files.newInputStream(Paths.get(path)), clazz, excelType);
|
|
|
} catch (IOException e) {
|
|
|
LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, BIZ_TYPE, CLASS_NAME
|
|
|
, String.format("excel文件解释失败 msg:%s", e.getMessage())
|
|
|
@@ -180,14 +182,14 @@ public class ExcelUtil {
|
|
|
*
|
|
|
* @param inputStream input流
|
|
|
* @param clazz 数据类型
|
|
|
- * @param excelEnum excel枚举
|
|
|
+ * @param excelType excel枚举
|
|
|
* @param <E> 数据泛形
|
|
|
* @return 数据集合
|
|
|
*/
|
|
|
- public static <E> Map<String, List<E>> parseExcel(InputStream inputStream, Class<E> clazz, ExcelEnum excelEnum) {
|
|
|
- if (inputStream == null || excelEnum == null || excelEnum == ExcelEnum.CSV) return null;
|
|
|
+ public static <E> Map<String, List<E>> parseExcel(InputStream inputStream, Class<E> clazz, ExcelType excelType) {
|
|
|
+ if (inputStream == null || excelType == null || excelType == ExcelType.CSV) return null;
|
|
|
//读取表
|
|
|
- try (Workbook workbook = excelEnum == ExcelEnum.XLSX ? new XSSFWorkbook(inputStream) : new HSSFWorkbook(inputStream)) {
|
|
|
+ try (Workbook workbook = excelType == ExcelType.XLSX ? new XSSFWorkbook(inputStream) : new HSSFWorkbook(inputStream)) {
|
|
|
if (workbook.getNumberOfSheets() <= 0) return null;
|
|
|
//结果
|
|
|
Map<String, List<E>> results = new HashMap<>(workbook.getNumberOfSheets());
|
|
|
@@ -275,12 +277,12 @@ public class ExcelUtil {
|
|
|
* @param dates 数据
|
|
|
* @param headers 表头信息
|
|
|
* @param outputStream 输出流
|
|
|
- * @param excelEnum excel枚举
|
|
|
+ * @param excelType excel枚举
|
|
|
*/
|
|
|
public static void writeExcel(Map<String, List<Map<String, Object>>> dates, Map<String, Map<String, String>> headers
|
|
|
- , OutputStream outputStream, ExcelEnum excelEnum) {
|
|
|
+ , OutputStream outputStream, ExcelType excelType) {
|
|
|
//创建表
|
|
|
- try (Workbook workbook = excelEnum == ExcelEnum.XLSX ? new SXSSFWorkbook() : new HSSFWorkbook()) {
|
|
|
+ try (Workbook workbook = excelType == ExcelType.XLSX ? new SXSSFWorkbook() : new HSSFWorkbook()) {
|
|
|
//表头样式
|
|
|
CellStyle headerStyle = workbook.createCellStyle();
|
|
|
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
@@ -316,11 +318,11 @@ public class ExcelUtil {
|
|
|
* @param dates 数据
|
|
|
* @param header 表头信息
|
|
|
* @param outputStream 输出流
|
|
|
- * @param excelEnum excel枚举
|
|
|
+ * @param excelType excel枚举
|
|
|
*/
|
|
|
- public static void writeExcel(List<Map<String, Object>> dates, Map<String, String> header, OutputStream outputStream, ExcelEnum excelEnum) {
|
|
|
+ public static void writeExcel(List<Map<String, Object>> dates, Map<String, String> header, OutputStream outputStream, ExcelType excelType) {
|
|
|
//创建表
|
|
|
- try (Workbook workbook = excelEnum == ExcelEnum.XLSX ? new XSSFWorkbook() : new HSSFWorkbook()) {
|
|
|
+ try (Workbook workbook = excelType == ExcelType.XLSX ? new XSSFWorkbook() : new HSSFWorkbook()) {
|
|
|
//表头样式
|
|
|
CellStyle headerStyle = workbook.createCellStyle();
|
|
|
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
@@ -387,12 +389,12 @@ public class ExcelUtil {
|
|
|
* @param dates 数据
|
|
|
* @param clazz 类型
|
|
|
* @param outputStream 输出流
|
|
|
- * @param excelEnum excel枚举
|
|
|
+ * @param excelType excel枚举
|
|
|
* @param <E> 数据泛形
|
|
|
*/
|
|
|
- public static <E> void writeExcel(List<E> dates, Class<E> clazz, OutputStream outputStream, ExcelEnum excelEnum) {
|
|
|
+ public static <E> void writeExcel(List<E> dates, Class<E> clazz, OutputStream outputStream, ExcelType excelType) {
|
|
|
//创建表
|
|
|
- try (Workbook workbook = excelEnum == ExcelEnum.XLSX ? new XSSFWorkbook() : new HSSFWorkbook()) {
|
|
|
+ try (Workbook workbook = excelType == ExcelType.XLSX ? new XSSFWorkbook() : new HSSFWorkbook()) {
|
|
|
//表头样式
|
|
|
CellStyle headerStyle = workbook.createCellStyle();
|
|
|
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
@@ -539,27 +541,96 @@ public class ExcelUtil {
|
|
|
* @param <E> 数据泛形
|
|
|
* @throws IllegalAccessException 非法访问异常
|
|
|
*/
|
|
|
- private static <E> void setObjValue(E data, Field field, Object value) throws IllegalAccessException {
|
|
|
+ private static <E> void setObjValue(E data, Field field, String value) throws IllegalAccessException {
|
|
|
+ //excel映射
|
|
|
+ ExcelMapping excelMapping = field.getAnnotation(ExcelMapping.class);
|
|
|
//值判断空
|
|
|
if (value != null) {
|
|
|
- //判断数据类型是否相同,如相同直接设置值,如不同,则转换值
|
|
|
+ //处理枚举取值
|
|
|
+ if (excelMapping != null && excelMapping.enumClass() != null && excelMapping.enumClass() != ExcelEnum.class) {
|
|
|
+ //获取枚举,并设置值
|
|
|
+ for (ExcelEnum e : excelMapping.enumClass().getEnumConstants())
|
|
|
+ if (Objects.equals(e.getLabel(), value)) {
|
|
|
+ value = e.getValue().toString();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //数据类型相同,直接设置值
|
|
|
if (field.getType().isAssignableFrom(value.getClass())) {
|
|
|
//设置值
|
|
|
field.set(data, value);
|
|
|
- } else {
|
|
|
- //如值为数字类型
|
|
|
- if (NumberUtils.isParsable(value.toString())) {
|
|
|
- //对数字类型兼容的类型处理
|
|
|
- if (field.getType() == short.class || field.getType() == Short.class)
|
|
|
- field.set(data, ((Double) Double.parseDouble(value.toString())).shortValue());
|
|
|
- else if (field.getType() == int.class || field.getType() == Integer.class)
|
|
|
- field.set(data, ((Double) Double.parseDouble(value.toString())).intValue());
|
|
|
- else if (field.getType() == long.class || field.getType() == Long.class)
|
|
|
- field.set(data, ((Double) Double.parseDouble(value.toString())).longValue());
|
|
|
- else if (field.getType() == float.class || field.getType() == Float.class)
|
|
|
- field.set(data, ((Double) Double.parseDouble(value.toString())).floatValue());
|
|
|
- else if (field.getType() == double.class || field.getType() == Double.class)
|
|
|
- field.set(data, (Double) Double.parseDouble(value.toString()));
|
|
|
+ }
|
|
|
+ //数据类型为数字类型
|
|
|
+ else if (NumberUtils.isParsable(value)) {
|
|
|
+ if (field.getType() == short.class || field.getType() == Short.class)
|
|
|
+ field.set(data, ((Double) Double.parseDouble(value)).shortValue());
|
|
|
+ else if (field.getType() == int.class || field.getType() == Integer.class)
|
|
|
+ field.set(data, ((Double) Double.parseDouble(value)).intValue());
|
|
|
+ else if (field.getType() == long.class || field.getType() == Long.class)
|
|
|
+ field.set(data, ((Double) Double.parseDouble(value)).longValue());
|
|
|
+ else if (field.getType() == float.class || field.getType() == Float.class)
|
|
|
+ field.set(data, ((Double) Double.parseDouble(value)).floatValue());
|
|
|
+ else if (field.getType() == double.class || field.getType() == Double.class)
|
|
|
+ field.set(data, (Double) Double.parseDouble(value));
|
|
|
+ else if (field.getType() == BigDecimal.class) field.set(data, new BigDecimal(value));
|
|
|
+ }
|
|
|
+ //字段类型为布尔
|
|
|
+ else if (field.getType() == boolean.class || field.getType() == Boolean.class) {
|
|
|
+ String lowerValue = value.toLowerCase();
|
|
|
+ if ("true".equals(lowerValue) || "是".equals(lowerValue) || "1".equals(lowerValue) || "y".equals(lowerValue) || "yes".equals(lowerValue)) {
|
|
|
+ field.set(data, true);
|
|
|
+ } else if ("false".equals(lowerValue) || "否".equals(lowerValue) || "0".equals(lowerValue) || "n".equals(lowerValue) || "no".equals(lowerValue)) {
|
|
|
+ field.set(data, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //字段类型为时间
|
|
|
+ else if (field.getType() == LocalDateTime.class || field.getType() == java.time.LocalDate.class || field.getType() == java.time.LocalTime.class ||
|
|
|
+ field.getType() == java.util.Date.class || field.getType() == java.sql.Date.class || field.getType() == java.sql.Timestamp.class) {
|
|
|
+ //常见的日期时间格式
|
|
|
+ List<String> patterns = Arrays.asList(
|
|
|
+ "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy.MM.dd HH:mm:ss",
|
|
|
+ "yyyy-MM-dd", "yyyy/MM/dd", "yyyy.MM.dd",
|
|
|
+ "HH:mm:ss", "HH:mm",
|
|
|
+ "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSS"
|
|
|
+ );
|
|
|
+ //遍历格式
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ try {
|
|
|
+ if (field.getType() == LocalDateTime.class) {
|
|
|
+ java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
|
|
|
+ LocalDateTime dateTime = LocalDateTime.parse(value, formatter);
|
|
|
+ field.set(data, dateTime);
|
|
|
+ return;
|
|
|
+ } else if (field.getType() == java.time.LocalDate.class) {
|
|
|
+ java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
|
|
|
+ java.time.LocalDate localDate = java.time.LocalDate.parse(value, formatter);
|
|
|
+ field.set(data, localDate);
|
|
|
+ return;
|
|
|
+ } else if (field.getType() == java.time.LocalTime.class) {
|
|
|
+ java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
|
|
|
+ java.time.LocalTime localTime = java.time.LocalTime.parse(value, formatter);
|
|
|
+ field.set(data, localTime);
|
|
|
+ return;
|
|
|
+ } else if (field.getType() == java.util.Date.class) {
|
|
|
+ java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern);
|
|
|
+ java.util.Date date = sdf.parse(value);
|
|
|
+ field.set(data, date);
|
|
|
+ return;
|
|
|
+ } else if (field.getType() == java.sql.Date.class) {
|
|
|
+ java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ java.util.Date utilDate = sdf.parse(value);
|
|
|
+ field.set(data, new java.sql.Date(utilDate.getTime()));
|
|
|
+ return;
|
|
|
+ } else if (field.getType() == java.sql.Timestamp.class) {
|
|
|
+ java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ java.util.Date utilDate = sdf.parse(value);
|
|
|
+ field.set(data, new java.sql.Timestamp(utilDate.getTime()));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|