ExcelTest.java 3.0 KB

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