|
@@ -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);
|
|
|
}
|
|
|
|
|
|
/**
|