mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-23 23:09:47 +08:00
update Excel写出包装器添加泛型用于限定write入参类型
This commit is contained in:
@ -212,7 +212,7 @@ public class ExcelUtil {
|
||||
* @param options Excel下拉可选项
|
||||
* @param consumer 导出助手消费函数
|
||||
*/
|
||||
public static <T> void exportExcel(Class<T> headType, OutputStream os, List<DropDownOptions> options, Consumer<ExcelWriterHelper> consumer) {
|
||||
public static <T> void exportExcel(Class<T> headType, OutputStream os, List<DropDownOptions> options, Consumer<ExcelWriterWrapper<T>> consumer) {
|
||||
try (ExcelWriter writer = FastExcel.write(os, headType)
|
||||
.autoCloseStream(false)
|
||||
// 自动适配
|
||||
@ -224,9 +224,8 @@ public class ExcelUtil {
|
||||
// 添加下拉框操作
|
||||
.registerWriteHandler(new ExcelDownHandler(options))
|
||||
.build()) {
|
||||
ExcelWriterHelper helper = ExcelWriterHelper.of(writer);
|
||||
// 执行消费函数
|
||||
consumer.accept(helper);
|
||||
consumer.accept(ExcelWriterWrapper.of(writer));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@ -239,7 +238,7 @@ public class ExcelUtil {
|
||||
* @param os 输出流
|
||||
* @param consumer 导出助手消费函数
|
||||
*/
|
||||
public static <T> void exportExcel(Class<T> headType, OutputStream os, Consumer<ExcelWriterHelper> consumer) {
|
||||
public static <T> void exportExcel(Class<T> headType, OutputStream os, Consumer<ExcelWriterWrapper<T>> consumer) {
|
||||
exportExcel(headType, os, null, consumer);
|
||||
}
|
||||
|
||||
|
@ -8,41 +8,33 @@ import cn.idev.excel.write.builder.ExcelWriterTableBuilder;
|
||||
import cn.idev.excel.write.metadata.WriteSheet;
|
||||
import cn.idev.excel.write.metadata.WriteTable;
|
||||
import cn.idev.excel.write.metadata.fill.FillConfig;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* ExcelWriterHelper Excel写出助手
|
||||
* ExcelWriterWrapper Excel写出包装器
|
||||
* <br>
|
||||
* 提供了一组与 ExcelWriter 一一对应的写出方法,避免直接提供 ExcelWriter 而导致的一些不可控问题(比如提前关闭了IO流等)
|
||||
*
|
||||
*
|
||||
* @see ExcelWriter
|
||||
* @author 秋辞未寒
|
||||
* @see ExcelWriter
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class ExcelWriterHelper {
|
||||
public record ExcelWriterWrapper<T>(ExcelWriter excelWriter) {
|
||||
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
private final ExcelWriter excelWriter;
|
||||
|
||||
public void write(Collection<?> data, WriteSheet writeSheet) {
|
||||
public void write(Collection<T> data, WriteSheet writeSheet) {
|
||||
excelWriter.write(data, writeSheet);
|
||||
}
|
||||
|
||||
public void write(Supplier<Collection<?>> supplier, WriteSheet writeSheet) {
|
||||
excelWriter.write(supplier, writeSheet);
|
||||
public void write(Supplier<Collection<T>> supplier, WriteSheet writeSheet) {
|
||||
excelWriter.write(supplier.get(), writeSheet);
|
||||
}
|
||||
|
||||
public void write(Collection<?> data, WriteSheet writeSheet, WriteTable writeTable) {
|
||||
public void write(Collection<T> data, WriteSheet writeSheet, WriteTable writeTable) {
|
||||
excelWriter.write(data, writeSheet, writeTable);
|
||||
}
|
||||
|
||||
public void write(Supplier<Collection<?>> supplier, WriteSheet writeSheet, WriteTable writeTable) {
|
||||
public void write(Supplier<Collection<T>> supplier, WriteSheet writeSheet, WriteTable writeTable) {
|
||||
excelWriter.write(supplier.get(), writeSheet, writeTable);
|
||||
}
|
||||
|
||||
@ -67,13 +59,13 @@ public class ExcelWriterHelper {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个 ExcelWriterHelper
|
||||
* 创建一个 ExcelWriterWrapper
|
||||
*
|
||||
* @param excelWriter ExcelWriter
|
||||
* @return ExcelWriterHelper
|
||||
* @return ExcelWriterWrapper
|
||||
*/
|
||||
public static ExcelWriterHelper of(ExcelWriter excelWriter) {
|
||||
return new ExcelWriterHelper(excelWriter);
|
||||
public static <T> ExcelWriterWrapper<T> of(ExcelWriter excelWriter) {
|
||||
return new ExcelWriterWrapper<>(excelWriter);
|
||||
}
|
||||
|
||||
// -------------------------------- sheet start
|
Reference in New Issue
Block a user