| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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.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.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<DictBO> 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<User> users = new ArrayList<>();
- for (int i = 0; i < 76; i++) {
- users.add(User.of(i,
- "用户"+(i/10),
- RandomUtil.randomInt(10,40),
- RandomUtil.randomBoolean()?"--":null,
- RandomUtil.randomBoolean()?"--":null,
- RandomUtil.randomString("1234",1)
- ));
- }
- long currentTimeMillis = System.currentTimeMillis();
- ExcelHelper excelHelper = excelHelperFactory.create(User.class);
- FileOutputStream fileOutputStream = new FileOutputStream(file);
- excelHelper.exportExcel(fileOutputStream,"111.xlsx",users,"测试","用户表单");
- System.out.println("耗时============"+(System.currentTimeMillis() - currentTimeMillis));
- }
- @Data
- @AllArgsConstructor(staticName = "of")
- private static class User{
- @Excel(name = "序号")
- private Integer sort;
- @Excel(name = "姓名",mergeRowWhenSame = true)
- private String name;
- @Excel(name = "年龄",suffix = "岁",mergeBlank = true)
- private Integer age;
- @Excel(name = "随机空格")
- private String blank;
- @Excel(name = "随机空格2")
- private String blank1;
- @Excel(name = "性别",dictCode = "gender",combo = {"男","女","未知"})
- private String gender;
- }
- @Getter
- @AllArgsConstructor
- static enum Gender implements IEnum<String>{
- man("1","男"),
- woman("2","女"),
- unkown("3","未知");
- private String value;
- private String label;
- }
- }
|