ソースを参照

Excel支持分割字符串组内容

RuoYi 5 年 前
コミット
9bfe3e5328

+ 5 - 0
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/annotation/Excel.java

@@ -35,6 +35,11 @@ public @interface Excel
35 35
     public String readConverterExp() default "";
36 36
 
37 37
     /**
38
+     * 分隔符,读取字符串组内容
39
+     */
40
+    public String separator() default ",";
41
+
42
+    /**
38 43
      * 导出类型(0数字 1字符串)
39 44
      */
40 45
     public ColumnType cellType() default ColumnType.STRING;

+ 41 - 24
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@@ -265,7 +265,7 @@ public class ExcelUtil<T>
265 265
                         }
266 266
                         else if (StringUtils.isNotEmpty(attr.readConverterExp()))
267 267
                         {
268
-                            val = reverseByExp(String.valueOf(val), attr.readConverterExp());
268
+                            val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
269 269
                         }
270 270
                         ReflectUtils.invokeSetter(entity, propertyName, val);
271 271
                     }
@@ -525,13 +525,14 @@ public class ExcelUtil<T>
525 525
                 Object value = getTargetValue(vo, field, attr);
526 526
                 String dateFormat = attr.dateFormat();
527 527
                 String readConverterExp = attr.readConverterExp();
528
+                String separator = attr.separator();
528 529
                 if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
529 530
                 {
530 531
                     cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
531 532
                 }
532 533
                 else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
533 534
                 {
534
-                    cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp));
535
+                    cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
535 536
                 }
536 537
                 else
537 538
                 {
@@ -609,28 +610,36 @@ public class ExcelUtil<T>
609 610
      * 
610 611
      * @param propertyValue 参数值
611 612
      * @param converterExp 翻译注解
613
+     * @param separator 分隔符
612 614
      * @return 解析后值
613
-     * @throws Exception
614 615
      */
615
-    public static String convertByExp(String propertyValue, String converterExp) throws Exception
616
+    public static String convertByExp(String propertyValue, String converterExp, String separator)
616 617
     {
617
-        try
618
+        StringBuilder propertyString = new StringBuilder();
619
+        String[] convertSource = converterExp.split(",");
620
+        for (String item : convertSource)
618 621
         {
619
-            String[] convertSource = converterExp.split(",");
620
-            for (String item : convertSource)
622
+            String[] itemArray = item.split("=");
623
+            if (StringUtils.containsAny(separator, propertyValue))
624
+            {
625
+                for (String value : propertyValue.split(separator))
626
+                {
627
+                    if (itemArray[0].equals(value))
628
+                    {
629
+                        propertyString.append(itemArray[1] + separator);
630
+                        break;
631
+                    }
632
+                }
633
+            }
634
+            else
621 635
             {
622
-                String[] itemArray = item.split("=");
623 636
                 if (itemArray[0].equals(propertyValue))
624 637
                 {
625 638
                     return itemArray[1];
626 639
                 }
627 640
             }
628 641
         }
629
-        catch (Exception e)
630
-        {
631
-            throw e;
632
-        }
633
-        return propertyValue;
642
+        return StringUtils.stripEnd(propertyString.toString(), separator);
634 643
     }
635 644
 
636 645
     /**
@@ -638,28 +647,36 @@ public class ExcelUtil<T>
638 647
      * 
639 648
      * @param propertyValue 参数值
640 649
      * @param converterExp 翻译注解
650
+     * @param separator 分隔符
641 651
      * @return 解析后值
642
-     * @throws Exception
643 652
      */
644
-    public static String reverseByExp(String propertyValue, String converterExp) throws Exception
653
+    public static String reverseByExp(String propertyValue, String converterExp, String separator)
645 654
     {
646
-        try
655
+        StringBuilder propertyString = new StringBuilder();
656
+        String[] convertSource = converterExp.split(",");
657
+        for (String item : convertSource)
647 658
         {
648
-            String[] convertSource = converterExp.split(",");
649
-            for (String item : convertSource)
659
+            String[] itemArray = item.split("=");
660
+            if (StringUtils.containsAny(separator, propertyValue))
661
+            {
662
+                for (String value : propertyValue.split(separator))
663
+                {
664
+                    if (itemArray[1].equals(value))
665
+                    {
666
+                        propertyString.append(itemArray[0] + separator);
667
+                        break;
668
+                    }
669
+                }
670
+            }
671
+            else
650 672
             {
651
-                String[] itemArray = item.split("=");
652 673
                 if (itemArray[1].equals(propertyValue))
653 674
                 {
654 675
                     return itemArray[0];
655 676
                 }
656 677
             }
657 678
         }
658
-        catch (Exception e)
659
-        {
660
-            throw e;
661
-        }
662
-        return propertyValue;
679
+        return StringUtils.stripEnd(propertyString.toString(), separator);
663 680
     }
664 681
 
665 682
     /**