Преглед на файлове

优化excel操作逻辑

woody преди 11 месеца
родител
ревизия
38cbcb2c29
променени са 1 файла, в които са добавени 90 реда и са изтрити 5 реда
  1. 90 5
      framework-common/src/main/java/com/chelvc/framework/common/util/ExcelUtils.java

+ 90 - 5
framework-common/src/main/java/com/chelvc/framework/common/util/ExcelUtils.java

@@ -19,6 +19,7 @@ import java.util.TreeSet;
 import java.util.function.BiFunction;
 
 import lombok.NonNull;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
 import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.ss.formula.FormulaParseException;
@@ -50,7 +51,7 @@ import org.xml.sax.helpers.XMLReaderFactory;
  * Excel工具类
  *
  * @author Woody
- * @date 2024/1/30
+ * @date 2024/4/3
  */
 public final class ExcelUtils {
     /**
@@ -167,6 +168,89 @@ public final class ExcelUtils {
         return ObjectUtils.ifNull(value, Object::toString);
     }
 
+    /**
+     * 读取Excel数据
+     *
+     * @param file   Excel文件
+     * @param reader Excel读接口
+     * @return 读取数量
+     */
+    public static int read(@NonNull File file, @NonNull Reader reader) {
+        return read(file, 0, reader);
+    }
+
+    /**
+     * 读取Excel数据
+     *
+     * @param file   Excel文件
+     * @param index  开始数据行下标(从0开始)
+     * @param reader Excel读接口
+     * @return 读取数量
+     */
+    public static int read(@NonNull File file, int index, @NonNull Reader reader) {
+        try {
+            return read(OPCPackage.open(file), index, reader);
+        } catch (InvalidFormatException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 读取Excel数据
+     *
+     * @param file   Excel文件
+     * @param reader Excel读接口
+     * @return 读取数量
+     */
+    public static int read(@NonNull String file, @NonNull Reader reader) {
+        return read(file, 0, reader);
+    }
+
+    /**
+     * 读取Excel数据
+     *
+     * @param file   Excel文件
+     * @param index  开始数据行下标(从0开始)
+     * @param reader Excel读接口
+     * @return 读取数量
+     */
+    public static int read(@NonNull String file, int index, @NonNull Reader reader) {
+        try {
+            return read(OPCPackage.open(file), index, reader);
+        } catch (InvalidFormatException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 读取Excel数据
+     *
+     * @param input  Excel输入流
+     * @param reader Excel读接口
+     * @return 读取数量
+     * @throws IOException I/O操作异常
+     */
+    public static int read(@NonNull InputStream input, @NonNull Reader reader) throws IOException {
+        return read(input, 0, reader);
+    }
+
+    /**
+     * 读取Excel数据
+     *
+     * @param input  Excel输入流
+     * @param index  开始数据行下标(从0开始)
+     * @param reader Excel读接口
+     * @return 读取数量
+     * @throws IOException I/O操作异常
+     */
+    public static int read(@NonNull InputStream input, int index, @NonNull Reader reader) throws IOException {
+        try {
+            return read(OPCPackage.open(input), index, reader);
+        } catch (InvalidFormatException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     /**
      * 通过解析XML的方式读取Excel数据
      *
@@ -174,7 +258,7 @@ public final class ExcelUtils {
      * @param reader Excel读接口
      * @return 读取数量
      */
-    public static int read(OPCPackage pkg, Reader reader) {
+    public static int read(@NonNull OPCPackage pkg, @NonNull Reader reader) {
         return read(pkg, 0, reader);
     }
 
@@ -293,7 +377,7 @@ public final class ExcelUtils {
                 break;
             }
             for (T object : objects) {
-                writer.write(sheet.createRow(index++), object, ++count);
+                index = writer.write(sheet.createRow(index), object, ++count) + 1;
             }
             if (objects.size() < size) {
                 break;
@@ -329,7 +413,7 @@ public final class ExcelUtils {
                     sheet = initializeSheet(workbook, titles);
                     row = origin;
                 }
-                writer.write(sheet.createRow(row++), object, index);
+                row = writer.write(sheet.createRow(row), object, index) + 1;
             }
             if (objects.size() < size) {
                 break;
@@ -378,8 +462,9 @@ public final class ExcelUtils {
          * @param row    数据行对象
          * @param object 对象实例
          * @param count  当前记录数(从1开始)
+         * @return 当前行号
          */
-        void write(Row row, T object, int count);
+        int write(Row row, T object, int count);
     }
 
     /**