package cn.tr.plugin.excel; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.RandomUtil; import cn.tr.core.enums.IEnum; import cn.tr.plugin.dict.annotation.Dict; import cn.tr.plugin.dict.bo.DictBO; import cn.tr.plugin.dict.config.cache.DictManager; import cn.tr.plugin.excel.annotation.Excel; import cn.tr.plugin.excel.config.ExcelHelper; import cn.tr.plugin.excel.config.ExcelHelperFactory; import cn.tr.plugin.test.ut.BaseMockitoUnitTest; import lombok.AllArgsConstructor; import lombok.Data; import lombok.Getter; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @ClassName : ExcelTest * @Description : * @Author : LF * @Date: 2023年03月13日 */ public class ExcelTest extends BaseMockitoUnitTest { private DictManager dictManager; private ExcelHelperFactory excelHelperFactory; private String pathPrefix="C:\\Users\\JR\\Desktop\\icon\\"; @BeforeEach public void setUp(){ dictManager=new DictManager(new ConcurrentMapCacheManager()); excelHelperFactory=new ExcelHelperFactory(dictManager); List dicts = Stream.of(Gender.values()) .map(gender -> { DictBO dict = new DictBO(); dict.setValue(gender.getValue()); dict.setLabel(gender.getLabel()); return dict; }) .collect(Collectors.toList()); dictManager.load("gender",dicts); } @Test public void exportExcel() throws Exception { File file = FileUtil.touch(pathPrefix + "111.xlsx"); ArrayList users = new ArrayList<>(); for (int i = 0; i < 100; i++) { users.add(User.of(i,"用户"+i, RandomUtil.randomString("123",1),RandomUtil.randomInt(10,40))); } long currentTimeMillis = System.currentTimeMillis(); ExcelHelper excelHelper = excelHelperFactory.create(User.class); FileOutputStream fileOutputStream = new FileOutputStream(file); excelHelper.exportExcel(fileOutputStream,users,"测试","用户表单"); System.out.println("耗时============"+(System.currentTimeMillis() - currentTimeMillis)); } @Data @AllArgsConstructor(staticName = "of") private static class User{ @Excel(name = "序号") private Integer sort; @Excel(name = "姓名") private String name; @Excel(name = "性别",dictCode = "gender") private String gender; @Excel(name = "年龄",suffix = "岁") private Integer age; } @Getter @AllArgsConstructor static enum Gender implements IEnum{ man("1","男"), woman("2","女"), unkown("3","未知"); private String value; private String label; } }