diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/DateUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/DateUtils.java index e297bb58..76fdaf85 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/DateUtils.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/DateUtils.java @@ -8,8 +8,10 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Date; import org.apache.commons.lang3.time.DateFormatUtils; +import org.springframework.util.StringUtils; /** * 时间工具类 @@ -127,6 +129,82 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils } } + /** + * 日期型字符串转化为日期 格式 + */ + public static LocalDate parseLocalDate(Object str) + { + if (str == null) + { + return null; + } + try + { + return toLocalDate(str.toString(), parsePatterns); + } + catch (ParseException e) + { + return null; + } + } + /** + * 日期型字符串转化为日期 格式 + */ + public static LocalDateTime parseLocalDateTime(Object str) + { + if (str == null) + { + return null; + } + try + { + return toLocalDateTime(str.toString(), parsePatterns); + } + catch (ParseException e) + { + return null; + } + } + public static LocalDate toLocalDate(String dateTime, String... formats) throws ParseException{ + if (StringUtils.isEmpty(dateTime)) { + return null; + } + if (formats == null || formats.length == 0) { + formats = new String[]{"yyyy-MM-dd"}; + } + for(String format:formats){ + try + { + DateTimeFormatter df = DateTimeFormatter.ofPattern(format); + LocalDate ldt = LocalDate.parse(dateTime, df); + return ldt; + }catch (Exception ignored) + { + + } + } + throw new ParseException("转换异常",formats.length); + } + public static LocalDateTime toLocalDateTime(String dateTime, String... formats) throws ParseException{ + if (StringUtils.isEmpty(dateTime)) { + return null; + } + if (formats == null || formats.length == 0) { + formats = new String[]{"yyyy-MM-dd HH:mm:ss"}; + } + for(String format:formats){ + try + { + DateTimeFormatter df = DateTimeFormatter.ofPattern(format); + LocalDateTime ldt = LocalDateTime.parse(dateTime, df); + return ldt; + }catch (Exception ignored) + { + + } + } + throw new ParseException("转换异常",formats.length); + } /** * 获取服务器启动时间 */ diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java index 6e0384d3..1eea190a 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java @@ -257,7 +257,7 @@ public class ExcelUtil /** * 对excel表单默认第一个索引名转换成list - * + * * @param is 输入流 * @return 转换后集合 */ @@ -282,7 +282,7 @@ public class ExcelUtil /** * 对excel表单默认第一个索引名转换成list - * + * * @param is 输入流 * @param titleNum 标题占用行数 * @return 转换后集合 @@ -292,9 +292,24 @@ public class ExcelUtil return importExcel(StringUtils.EMPTY, is, titleNum); } + /** + * 初始excel值转换为字段的转换器 + * @return 值和字段的映射表 + */ + public Map initValueToFieldConversionMap(Excel attr){ + HashMap conversionMap = new HashMap<>(); + String allList = attr.readConverterExp(); + String[] split = allList.split(","); + for (String dictTagItem : split) + { + String[] split1 = dictTagItem.split("="); + conversionMap.put(split1[1], split1[0]); + } + return conversionMap; + } /** * 对excel表单指定表格索引名转换成list - * + * * @param sheetName 表格索引名 * @param titleNum 标题占用行数 * @param is 输入流 @@ -336,6 +351,8 @@ public class ExcelUtil // 有数据时才处理 得到类的所有field. List fields = this.getFields(); Map fieldsMap = new HashMap(); + // 定义所有的excel值和java对象的值的转换映射表 + Map> allFieldConversionMap=new HashMap<>(); for (Object[] objects : fields) { Excel attr = (Excel) objects[1]; @@ -343,6 +360,11 @@ public class ExcelUtil if (column != null) { fieldsMap.put(column, objects); + //如果存在解析键值映射,则直接初始化 + if(StringUtils.isNotEmpty(attr.readConverterExp())){ + Map fieldConversionMap = initValueToFieldConversionMap(attr); + allFieldConversionMap.put(attr.name(),fieldConversionMap); + } } } for (int i = titleNum + 1; i <= rows; i++) @@ -417,6 +439,14 @@ public class ExcelUtil val = DateUtil.getJavaDate((Double) val); } } + else if (LocalDate.class == fieldType) + { + val = DateUtils.parseLocalDate(val); + } + else if (LocalDateTime.class == fieldType) + { + val = DateUtils.parseLocalDateTime(val); + } else if (Boolean.TYPE == fieldType || Boolean.class == fieldType) { val = Convert.toBool(val, false); @@ -428,9 +458,9 @@ public class ExcelUtil { propertyName = field.getName() + "." + attr.targetAttr(); } - if (StringUtils.isNotEmpty(attr.readConverterExp())) + if (allFieldConversionMap.get(attr.name())!=null) { - val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator()); + val = getConvertValueByExpMap(Convert.toStr(val), attr.separator(), allFieldConversionMap.get(attr.name())); } else if (!attr.handler().equals(ExcelHandlerAdapter.class)) { @@ -503,7 +533,7 @@ public class ExcelUtil /** * 对list数据源将其里面的数据导入到excel表单 - * + * * @return 结果 */ public void exportExcel(HttpServletResponse response) @@ -533,7 +563,8 @@ public class ExcelUtil for (int index = 0; index < sheetNo; index++) { createSheet(sheetNo, index); - + //字段转换列表<列下标,<待转值,转换值>> + Map> fieldConversionMap = new HashMap<>(); // 产生一行 Row row = sheet.createRow(rownum); int column = 0; @@ -547,17 +578,17 @@ public class ExcelUtil for (Field subField : subFields) { Excel subExcel = subField.getAnnotation(Excel.class); - this.createHeadCell(subExcel, row, column++); + this.createHeadCellAndInitFieldConversion(subExcel, row, column++,fieldConversionMap); } } else { - this.createHeadCell(excel, row, column++); + this.createHeadCellAndInitFieldConversion(excel, row, column++,fieldConversionMap); } } if (Type.EXPORT.equals(type)) { - fillExcelData(index, row); + fillExcelData(index, row, fieldConversionMap); addStatisticsRow(); } } @@ -565,12 +596,13 @@ public class ExcelUtil /** * 填充excel数据 - * + * * @param index 序号 * @param row 单元格行 + * @param fieldConversionMap 字段转换Map */ @SuppressWarnings("unchecked") - public void fillExcelData(int index, Row row) + public void fillExcelData(int index, Row row, Map> fieldConversionMap) { int startNo = index * sheetSize; int endNo = Math.min(startNo + sheetSize, list.size()); @@ -618,7 +650,7 @@ public class ExcelUtil { subField.setAccessible(true); Excel attr = subField.getAnnotation(Excel.class); - this.addCell(attr, row, (T) obj, subField, column + subIndex); + this.addCell(attr, row, (T) obj, subField, column + subIndex, fieldConversionMap); } subIndex++; } @@ -628,7 +660,7 @@ public class ExcelUtil } else { - this.addCell(excel, row, vo, field, column++); + this.addCell(excel, row, vo, field, column++, fieldConversionMap); } } } @@ -636,7 +668,7 @@ public class ExcelUtil /** * 创建表格样式 - * + * * @param wb 工作薄对象 * @return 样式列表 */ @@ -689,7 +721,7 @@ public class ExcelUtil /** * 根据Excel注解创建表格头样式 - * + * * @param wb 工作薄对象 * @return 自定义样式列表 */ @@ -722,7 +754,7 @@ public class ExcelUtil /** * 根据Excel注解创建表格列样式 - * + * * @param wb 工作薄对象 * @return 自定义样式列表 */ @@ -760,9 +792,9 @@ public class ExcelUtil } /** - * 创建单元格 + * 创建首行单元格,并且初始化字段的转换 */ - public Cell createHeadCell(Excel attr, Row row, int column) + public Cell createHeadCellAndInitFieldConversion(Excel attr, Row row, int column, Map> fieldConversionMap) { // 创建列 Cell cell = row.createCell(column); @@ -770,6 +802,19 @@ public class ExcelUtil cell.setCellValue(attr.name()); setDataValidation(attr, row, column); cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); + //字段转换初始化(通过readConverterExp字符转换) + if (StringUtils.isNotEmpty(attr.readConverterExp())) + { + HashMap conversionMap = new HashMap<>(); + String allList = attr.readConverterExp(); + String[] split = allList.split(","); + for (String dictTagItem : split) + { + String[] split1 = dictTagItem.split("="); + conversionMap.put(split1[0], split1[1]); + } + fieldConversionMap.put(column, conversionMap); + } if (isSubList()) { // 填充默认样式,防止合并单元格样式失效 @@ -784,7 +829,7 @@ public class ExcelUtil /** * 设置单元格信息 - * + * * @param value 单元格值 * @param attr 注解相关 * @param cell 单元格信息 @@ -885,8 +930,15 @@ public class ExcelUtil /** * 添加单元格 + * @param attr 当前单元格表注解 + * @param row 当前单元格归属行 + * @param vo 单元格内容 + * @param field 单元格字段 + * @param column 单元格的列号 + * @param fieldConversionMap 所有单元格的解析值字典 + * @return 生成后的单元格 */ - public Cell addCell(Excel attr, Row row, T vo, Field field, int column) + public Cell addCell(Excel attr, Row row, T vo, Field field, int column,Map> fieldConversionMap) { Cell cell = null; try @@ -908,15 +960,14 @@ public class ExcelUtil // 用于读取对象中的属性 Object value = getTargetValue(vo, field, attr); String dateFormat = attr.dateFormat(); - String readConverterExp = attr.readConverterExp(); String separator = attr.separator(); if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) { cell.setCellValue(parseDateToStr(dateFormat, value)); } - else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)) + else if (fieldConversionMap.get(column)!=null && StringUtils.isNotNull(value)) { - cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator)); + cell.setCellValue(getConvertValueByExpMap(Convert.toStr(value), separator, fieldConversionMap.get(column))); } else if (value instanceof BigDecimal && -1 != attr.scale()) { @@ -943,7 +994,7 @@ public class ExcelUtil /** * 设置 POI XSSFSheet 单元格提示或选择框 - * + * * @param sheet 表单 * @param textlist 下拉框显示的内容 * @param promptContent 提示内容 @@ -980,7 +1031,7 @@ public class ExcelUtil /** * 设置某些列的值只能输入预制的数据,显示下拉框(兼容超出一定数量的下拉框). - * + * * @param sheet 要设置的sheet. * @param textlist 下拉框显示的内容 * @param promptContent 提示内容 @@ -1031,77 +1082,34 @@ public class ExcelUtil } /** - * 解析导出值 0=男,1=女,2=未知 + * 通过解析字典,寻找解析值 * - * @param propertyValue 参数值 - * @param converterExp 翻译注解 - * @param separator 分隔符 + * @param propertyValue 解析前的值 + * @param separator 分隔符 + * @param conversionMap 解析字典 * @return 解析后值 */ - public static String convertByExp(String propertyValue, String converterExp, String separator) - { - StringBuilder propertyString = new StringBuilder(); - String[] convertSource = converterExp.split(","); - for (String item : convertSource) + public static String getConvertValueByExpMap(String propertyValue,String separator,Map conversionMap){ + List afterConversionValueList=new ArrayList<>(); + //如果某个对象值为"1,2,3",则根据转换Map表全都进修转换 + if (StringUtils.containsAny(propertyValue, separator)) { - String[] itemArray = item.split("="); - if (StringUtils.containsAny(propertyValue, separator)) + for (String value : propertyValue.split(separator)) { - for (String value : propertyValue.split(separator)) + String conversionValue = conversionMap.get(value); + if(StringUtils.isNotEmpty(conversionValue)) { - if (itemArray[0].equals(value)) - { - propertyString.append(itemArray[1] + separator); - break; - } + afterConversionValueList.add(conversionValue); } } - else - { - if (itemArray[0].equals(propertyValue)) - { - return itemArray[1]; - } - } - } - return StringUtils.stripEnd(propertyString.toString(), separator); - } - - /** - * 反向解析值 男=0,女=1,未知=2 - * - * @param propertyValue 参数值 - * @param converterExp 翻译注解 - * @param separator 分隔符 - * @return 解析后值 - */ - public static String reverseByExp(String propertyValue, String converterExp, String separator) - { - StringBuilder propertyString = new StringBuilder(); - String[] convertSource = converterExp.split(","); - for (String item : convertSource) + }else { - String[] itemArray = item.split("="); - if (StringUtils.containsAny(propertyValue, separator)) + if(StringUtils.isNotEmpty(conversionMap.get(propertyValue))) { - for (String value : propertyValue.split(separator)) - { - if (itemArray[1].equals(value)) - { - propertyString.append(itemArray[0] + separator); - break; - } - } - } - else - { - if (itemArray[1].equals(propertyValue)) - { - return itemArray[0]; - } + afterConversionValueList.add(conversionMap.get(propertyValue)); } } - return StringUtils.stripEnd(propertyString.toString(), separator); + return String.join(separator,afterConversionValueList); } /** @@ -1311,7 +1319,7 @@ public class ExcelUtil /** * 创建工作表 - * + * * @param sheetNo sheet数量 * @param index 序号 */ @@ -1328,7 +1336,7 @@ public class ExcelUtil /** * 获取单元格值 - * + * * @param row 获取的行 * @param column 获取单元格列号 * @return 单元格值 @@ -1388,7 +1396,7 @@ public class ExcelUtil /** * 判断是否是空行 - * + * * @param row 判断的行 * @return */ @@ -1411,7 +1419,7 @@ public class ExcelUtil /** * 格式化不同类型的日期对象 - * + * * @param dateFormat 日期格式 * @param val 被格式化的日期对象 * @return 格式化后的日期字符 @@ -1477,7 +1485,7 @@ public class ExcelUtil /** * 获取对象的子列表方法 - * + * * @param name 名称 * @param pojoClass 类对象 * @return 子列表方法