|
|
|
|
@ -3,13 +3,13 @@ package org.dromara.common.excel.utils;
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.io.resource.ClassPathResource;
|
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
|
|
import com.alibaba.excel.ExcelWriter;
|
|
|
|
|
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
|
|
|
|
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
|
|
|
|
import com.alibaba.excel.write.metadata.fill.FillConfig;
|
|
|
|
|
import com.alibaba.excel.write.metadata.fill.FillWrapper;
|
|
|
|
|
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
|
|
|
|
import cn.idev.excel.FastExcel;
|
|
|
|
|
import cn.idev.excel.ExcelWriter;
|
|
|
|
|
import cn.idev.excel.write.builder.ExcelWriterSheetBuilder;
|
|
|
|
|
import cn.idev.excel.write.metadata.WriteSheet;
|
|
|
|
|
import cn.idev.excel.write.metadata.fill.FillConfig;
|
|
|
|
|
import cn.idev.excel.write.metadata.fill.FillWrapper;
|
|
|
|
|
import cn.idev.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
|
|
|
|
import jakarta.servlet.ServletOutputStream;
|
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
import lombok.AccessLevel;
|
|
|
|
|
@ -43,7 +43,7 @@ public class ExcelUtil {
|
|
|
|
|
* @return 转换后集合
|
|
|
|
|
*/
|
|
|
|
|
public static <T> List<T> importExcel(InputStream is, Class<T> clazz) {
|
|
|
|
|
return EasyExcel.read(is).head(clazz).autoCloseStream(false).sheet().doReadSync();
|
|
|
|
|
return FastExcel.read(is).head(clazz).autoCloseStream(false).sheet().doReadSync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -57,7 +57,7 @@ public class ExcelUtil {
|
|
|
|
|
*/
|
|
|
|
|
public static <T> ExcelResult<T> importExcel(InputStream is, Class<T> clazz, boolean isValidate) {
|
|
|
|
|
DefaultExcelListener<T> listener = new DefaultExcelListener<>(isValidate);
|
|
|
|
|
EasyExcel.read(is, clazz, listener).sheet().doRead();
|
|
|
|
|
FastExcel.read(is, clazz, listener).sheet().doRead();
|
|
|
|
|
return listener.getExcelResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -70,7 +70,7 @@ public class ExcelUtil {
|
|
|
|
|
* @return 转换后集合
|
|
|
|
|
*/
|
|
|
|
|
public static <T> ExcelResult<T> importExcel(InputStream is, Class<T> clazz, ExcelListener<T> listener) {
|
|
|
|
|
EasyExcel.read(is, clazz, listener).sheet().doRead();
|
|
|
|
|
FastExcel.read(is, clazz, listener).sheet().doRead();
|
|
|
|
|
return listener.getExcelResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -186,7 +186,7 @@ public class ExcelUtil {
|
|
|
|
|
*/
|
|
|
|
|
public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge,
|
|
|
|
|
OutputStream os, List<DropDownOptions> options) {
|
|
|
|
|
ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz)
|
|
|
|
|
ExcelWriterSheetBuilder builder = FastExcel.write(os, clazz)
|
|
|
|
|
.autoCloseStream(false)
|
|
|
|
|
// 自动适配
|
|
|
|
|
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
|
|
|
|
@ -237,14 +237,14 @@ public class ExcelUtil {
|
|
|
|
|
*/
|
|
|
|
|
public static <T> void exportTemplate(List<T> data, String templatePath, OutputStream os) {
|
|
|
|
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
|
|
|
|
ExcelWriter excelWriter = FastExcel.write(os)
|
|
|
|
|
.withTemplate(templateResource.getStream())
|
|
|
|
|
.autoCloseStream(false)
|
|
|
|
|
// 大数值自动转换 防止失真
|
|
|
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
|
|
|
.registerWriteHandler(new DataWriteHandler(data.get(0).getClass()))
|
|
|
|
|
.build();
|
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
|
|
|
|
WriteSheet writeSheet = FastExcel.writerSheet().build();
|
|
|
|
|
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
|
|
|
|
|
// 单表多数据导出 模板格式为 {.属性}
|
|
|
|
|
for (T d : data) {
|
|
|
|
|
@ -310,13 +310,13 @@ public class ExcelUtil {
|
|
|
|
|
*/
|
|
|
|
|
public static void exportTemplateMultiList(Map<String, Object> data, String templatePath, OutputStream os) {
|
|
|
|
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
|
|
|
|
ExcelWriter excelWriter = FastExcel.write(os)
|
|
|
|
|
.withTemplate(templateResource.getStream())
|
|
|
|
|
.autoCloseStream(false)
|
|
|
|
|
// 大数值自动转换 防止失真
|
|
|
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
|
|
|
.build();
|
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
|
|
|
|
WriteSheet writeSheet = FastExcel.writerSheet().build();
|
|
|
|
|
for (Map.Entry<String, Object> map : data.entrySet()) {
|
|
|
|
|
// 设置列表后续还有数据
|
|
|
|
|
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
|
|
|
|
|
@ -341,14 +341,14 @@ public class ExcelUtil {
|
|
|
|
|
*/
|
|
|
|
|
public static void exportTemplateMultiSheet(List<Map<String, Object>> data, String templatePath, OutputStream os) {
|
|
|
|
|
ClassPathResource templateResource = new ClassPathResource(templatePath);
|
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(os)
|
|
|
|
|
ExcelWriter excelWriter = FastExcel.write(os)
|
|
|
|
|
.withTemplate(templateResource.getStream())
|
|
|
|
|
.autoCloseStream(false)
|
|
|
|
|
// 大数值自动转换 防止失真
|
|
|
|
|
.registerConverter(new ExcelBigNumberConvert())
|
|
|
|
|
.build();
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
|
WriteSheet writeSheet = EasyExcel.writerSheet(i).build();
|
|
|
|
|
WriteSheet writeSheet = FastExcel.writerSheet(i).build();
|
|
|
|
|
for (Map.Entry<String, Object> map : data.get(i).entrySet()) {
|
|
|
|
|
// 设置列表后续还有数据
|
|
|
|
|
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
|
|
|
|
|
|