- 相关依赖 pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.7</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
- EasyExcelWriteUtils
import cn.hutool.core.util.PageUtil;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
/**
* EasyExcel写入工具类
*
* @author firefly_
* @since 2021/12/26
*/
@Slf4j
public class EasyExcelWriteUtils {
private EasyExcelWriteUtils() {
}
/**
* 分页写入
*
* @param dao dao extends IService
* @param countFunction 查询总数的方法
* @param queryFunction 分页查询的方法
* @param pageSize 分页大小
* @param excelWriter writer
* @param writeSheet sheet
* @param headClass 导出文件表头类
* @param <S>
* @param <T>
*/
public static <S extends IService<T>, T> void pageWrite(S dao,
Function<S, Integer> countFunction, BiFunction<S, IPage<?>, ?> queryFunction,
Integer pageSize, ExcelWriter excelWriter, WriteSheet writeSheet, Class<?> headClass) {
// 查询数据总数
Integer totalCount = countFunction.apply(dao);
if (null == totalCount || 0 == totalCount) {
// 写入空数据到sheet
excelWriter.write(new ArrayList<>(), writeSheet);
return;
}
// 计算总页数
int totalPage = PageUtil.totalPage(totalCount, pageSize);
IntStream.rangeClosed(1, totalPage).boxed()
.forEach(e -> {
List<?> data = new ArrayList<>();
// 执行分页查询,返回类型可能是List,可能是Page
Object result = queryFunction.apply(dao, new Page<>(e, pageSize, false));
if (result.getClass().equals(ArrayList.class)) {
// 转换成表头类
data = copyList((List<?>) result, headClass);
} else if (result.getClass().equals(Page.class)) {
// 转换成表头类
data = copyList(((Page<?>) result).getRecords(), headClass);
}
// 执行写入
excelWriter.write(data, writeSheet);
});
// 如果是往同一个文件写入多个sheet,这一步操作放在方法外面
excelWriter.finish();
}
/**
* 对象属性复制
*
* @param sourceObj
* @param targetCls
* @param <T>
* @return
*/
private static <T> T copyBean(Object sourceObj, Class<T> targetCls) {
T targetObj = null;
if (sourceObj != null) {
try {
targetObj = targetCls.newInstance();
BeanUtils.copyProperties(sourceObj, targetObj);
} catch (Exception e) {
e.printStackTrace();
}
}
return targetObj;
}
/**
* 集合属性复制
*
* @param list
* @param targetCls
* @param <S>
* @param <R>
* @return
*/
private static <S, R> List<R> copyList(Collection<S> list, Class<R> targetCls) {
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
return list.stream().map(obj -> copyBean(obj, targetCls))
.collect(Collectors.toList());
}
}