ExcelTest.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package cn.tr.plugin.excel;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.util.RandomUtil;
  4. import cn.tr.core.enums.IEnum;
  5. import cn.tr.plugin.dict.bo.DictBO;
  6. import cn.tr.plugin.dict.config.cache.DictManager;
  7. import cn.tr.plugin.excel.annotation.Excel;
  8. import cn.tr.plugin.excel.config.ExcelHelper;
  9. import cn.tr.plugin.excel.config.ExcelHelperFactory;
  10. import cn.tr.plugin.test.ut.BaseMockitoUnitTest;
  11. import lombok.AllArgsConstructor;
  12. import lombok.Data;
  13. import lombok.Getter;
  14. import org.junit.jupiter.api.BeforeEach;
  15. import org.junit.jupiter.api.Test;
  16. import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.stream.Collectors;
  22. import java.util.stream.Stream;
  23. /**
  24. * @ClassName : ExcelTest
  25. * @Description :
  26. * @Author : LF
  27. * @Date: 2023年03月13日
  28. */
  29. public class ExcelTest extends BaseMockitoUnitTest {
  30. private DictManager dictManager;
  31. private ExcelHelperFactory excelHelperFactory;
  32. private String pathPrefix="C:\\Users\\JR\\Desktop\\icon\\";
  33. @BeforeEach
  34. public void setUp(){
  35. dictManager=new DictManager(new ConcurrentMapCacheManager());
  36. excelHelperFactory=new ExcelHelperFactory(dictManager);
  37. List<DictBO> dicts = Stream.of(Gender.values())
  38. .map(gender -> {
  39. DictBO dict = new DictBO();
  40. dict.setValue(gender.getValue());
  41. dict.setLabel(gender.getLabel());
  42. return dict;
  43. })
  44. .collect(Collectors.toList());
  45. dictManager.load("gender",dicts);
  46. }
  47. @Test
  48. public void exportExcel() throws Exception {
  49. File file = FileUtil.touch(pathPrefix + "111.xlsx");
  50. ArrayList<User> users = new ArrayList<>();
  51. for (int i = 0; i < 76; i++) {
  52. users.add(User.of(i,
  53. "用户"+(i/10),
  54. RandomUtil.randomInt(10,40),
  55. RandomUtil.randomBoolean()?"--":null,
  56. RandomUtil.randomBoolean()?"--":null,
  57. RandomUtil.randomString("1234",1)
  58. ));
  59. }
  60. long currentTimeMillis = System.currentTimeMillis();
  61. ExcelHelper excelHelper = excelHelperFactory.create(User.class);
  62. FileOutputStream fileOutputStream = new FileOutputStream(file);
  63. excelHelper.exportExcel(fileOutputStream,"111.xlsx",users,"测试","用户表单");
  64. System.out.println("耗时============"+(System.currentTimeMillis() - currentTimeMillis));
  65. }
  66. @Data
  67. @AllArgsConstructor(staticName = "of")
  68. private static class User{
  69. @Excel(name = "序号")
  70. private Integer sort;
  71. @Excel(name = "姓名",mergeRowWhenSame = true)
  72. private String name;
  73. @Excel(name = "年龄",suffix = "岁",mergeBlank = true)
  74. private Integer age;
  75. @Excel(name = "随机空格")
  76. private String blank;
  77. @Excel(name = "随机空格2")
  78. private String blank1;
  79. @Excel(name = "性别",dictCode = "gender",combo = {"男","女","未知"})
  80. private String gender;
  81. }
  82. @Getter
  83. @AllArgsConstructor
  84. static enum Gender implements IEnum<String>{
  85. man("1","男"),
  86. woman("2","女"),
  87. unkown("3","未知");
  88. private String value;
  89. private String label;
  90. }
  91. }