• hutool、esayPoi、easyExcel、读写数据,性能对比


    目录

    1.源码下载 

    2、先上结论

    3、代码

    3.1、实体、接口

    3.2、读CSV

    3.3、读Excel

    3.4、写CSV

    3.5、写Excel

    3.6、业务示例


    1.源码下载 

    示例源码下载(含模板)

    2、先上结论

    1. 注意注意::::导出大文件的时候可能会有临时存储,
    2. 我的是在C:\Users\xbx\AppData\Local\Temp\poifiles
    3. 读csv文件
    4. 行数:1048575
    5. hutool: 5205
    6. esayPoi: 3929
    7. easyExcel1: 1917
    8. easyExcel2: 2832
    9. 读excel文件
    10. 行数:1048575
    11. hutool: 58207
    12. esayPoi: 25513
    13. easyExce1: 10646
    14. easyExcel2: 10123
    15. 写csv文件:
    16. 行数:1048575
    17. hutool: 6136
    18. esayPoi: 828
    19. easyExcel1: 6943
    20. easyExcel2: 5212
    21. 写excel文件:
    22. 行数:1048575
    23. hutool1: 146655
    24. hutool2: 23570
    25. esayPoi1: 21805
    26. esayPoi2: 93034
    27. easyExcel1:20762
    28. easyExcel2:20523
    29. easyExcel3:15386
    30. easyExcel4:14586

    3、代码

    3.1、实体、接口

    1. package cn.sdjtky.ltpon.FileTest;
    2. import cn.afterturn.easypoi.excel.annotation.Excel;
    3. import lombok.Data;
    4. /**
    5. * @version v1.0
    6. * @ProjectName: ltpon
    7. * @ClassName: Strainentey
    8. * @Description:
    9. * @Author: xbx
    10. * @Date: 2023/12/8 13:55
    11. */
    12. @Data
    13. public class DeviceDto {
    14. //sn+channel
    15. String sn;
    16. //与地表距离(m)
    17. Double surfaceDistance;
    18. //结构层位
    19. String structuralLayer;
    20. //就写电信号
    21. String channel;
    22. //这个值就是StressStrainDataDto里面的key 如:"t.v1","t.v2"
    23. String dataKey;
    24. }
    1. package cn.sdjtky.ltpon.FileTest;
    2. import cn.hutool.core.annotation.Alias;
    3. import lombok.Data;
    4. /**
    5. * @version v1.0
    6. * @ProjectName: ltpon
    7. * @ClassName: Strainentey
    8. * @Description:
    9. * @Author: xbx
    10. * @Date: 2023/12/8 13:55
    11. */
    12. @Data
    13. public class StrainEntity {
    14. @Alias("时间")
    15. String time;
    16. @Alias("通道1")
    17. String v1;
    18. @Alias("通道2")
    19. String v2;
    20. @Alias("通道3")
    21. String v3;
    22. @Alias("通道4")
    23. String v4;
    24. Double v5;
    25. Double v6;
    26. Double v7;
    27. Double v8;
    28. }
    1. package cn.sdjtky.ltpon.FileTest;
    2. import cn.afterturn.easypoi.excel.annotation.Excel;
    3. import lombok.Data;
    4. /**
    5. * @version v1.0
    6. * @ProjectName: ltpon
    7. * @ClassName: Strainentey
    8. * @Description:
    9. * @Author: xbx
    10. * @Date: 2023/12/8 13:55
    11. */
    12. @Data
    13. public class StrainEntity2 {
    14. @Excel(name = "时间")
    15. String time;
    16. @Excel(name = "通道1")
    17. String v1;
    18. @Excel(name = "通道2")
    19. String v2;
    20. @Excel(name = "通道3")
    21. String v3;
    22. @Excel(name = "通道4")
    23. String v4;
    24. Double v5;
    25. Double v6;
    26. Double v7;
    27. Double v8;
    28. }
    1. package cn.sdjtky.ltpon.FileTest;
    2. import com.alibaba.excel.annotation.ExcelProperty;
    3. import lombok.Data;
    4. /**
    5. * @version v1.0
    6. * @ProjectName: ltpon
    7. * @ClassName: Strainentey
    8. * @Description:
    9. * @Author: xbx
    10. * @Date: 2023/12/8 13:55
    11. */
    12. @Data
    13. public class StrainEntity3 {
    14. @ExcelProperty("时间")
    15. String time;
    16. @ExcelProperty("通道1")
    17. String v1;
    18. @ExcelProperty("通道2")
    19. String v2;
    20. @ExcelProperty("通道3")
    21. String v3;
    22. @ExcelProperty("通道4")
    23. String v4;
    24. Double v5;
    25. Double v6;
    26. Double v7;
    27. Double v8;
    28. }
    1. package cn.sdjtky.ltpon.FileTest;
    2. import cn.afterturn.easypoi.excel.annotation.Excel;
    3. import lombok.Data;
    4. import java.util.Date;
    5. /**
    6. * @version v1.0
    7. * @ProjectName: ltpon
    8. * @ClassName: Strainentey
    9. * @Description:
    10. * @Author: xbx
    11. * @Date: 2023/12/8 13:55
    12. */
    13. @Data
    14. public class StressStrainDataDto {
    15. // 年/月/日
    16. String date;
    17. // 时/分/秒/毫秒
    18. String time;
    19. // 时/分/秒/毫秒
    20. String dateTime;
    21. Double v1;
    22. Double v2;
    23. Double v3;
    24. Double v4;
    25. Double v5;
    26. Double v6;
    27. Double v7;
    28. Double v8;
    29. Double v9;
    30. Double v10;
    31. Double v11;
    32. Double v12;
    33. Double v13;
    34. }
    1. package cn.sdjtky.ltpon.FileTest;
    2. import cn.sdjtky.ltpon.FileTest.StrainEntity3;
    3. import com.alibaba.excel.context.AnalysisContext;
    4. import com.alibaba.excel.read.listener.ReadListener;
    5. import com.alibaba.excel.util.ListUtils;
    6. import com.alibaba.fastjson2.JSON;
    7. import org.apache.poi.ss.formula.functions.T;
    8. import java.util.ArrayList;
    9. import java.util.List;
    10. // 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
    11. public class Strain3ReadListener implements ReadListener {
    12. /**
    13. * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
    14. */
    15. /**
    16. * 缓存的数据
    17. */
    18. private List cachedDataList = new ArrayList();
    19. /**
    20. * 这个每一条数据解析都会来调用
    21. *
    22. * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()}
    23. * @param context
    24. */
    25. @Override
    26. public void invoke(T data, AnalysisContext context) {
    27. cachedDataList.add(data);
    28. // // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
    29. // if (cachedDataList.size() >= BATCH_COUNT) {
    30. // // 存储完成清理 list
    31. // cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    32. // }
    33. }
    34. /**
    35. * 所有数据解析完成了 都会来调用
    36. *
    37. * @param context
    38. */
    39. @Override
    40. public void doAfterAllAnalysed(AnalysisContext context) {
    41. // 这里也要保存数据,确保最后遗留的数据也存储到数据库
    42. }
    43. public List getCachedDataList() {
    44. return cachedDataList;
    45. }
    46. }

    3.2、读CSV

    1. package cn.sdjtky.ltpon.FileTest;
    2. import cn.afterturn.easypoi.csv.CsvImportUtil;
    3. import cn.afterturn.easypoi.csv.entity.CsvImportParams;
    4. import cn.afterturn.easypoi.excel.entity.ImportParams;
    5. import cn.hutool.core.io.resource.ResourceUtil;
    6. import cn.hutool.core.text.csv.CsvReader;
    7. import cn.hutool.core.text.csv.CsvUtil;
    8. import com.alibaba.excel.EasyExcel;
    9. import com.alibaba.excel.context.AnalysisContext;
    10. import com.alibaba.excel.event.AnalysisEventListener;
    11. import com.alibaba.excel.support.ExcelTypeEnum;
    12. import org.junit.jupiter.api.Test;
    13. import org.slf4j.Logger;
    14. import org.slf4j.LoggerFactory;
    15. import java.io.File;
    16. import java.io.FileInputStream;
    17. import java.util.ArrayList;
    18. import java.util.List;
    19. public class ReadCsvTest {
    20. private static final Logger LOGGER = LoggerFactory.getLogger(ReadCsvTest.class);
    21. @Test
    22. public void test() {
    23. String filePath = "G:/test/1048575.csv";
    24. hutool(filePath);
    25. // esayPoi(filePath);
    26. // easyExcel1(filePath);
    27. // easyExcel2(filePath);
    28. }
    29. public void hutool(String filePath) {
    30. try {
    31. long start = System.currentTimeMillis();
    32. CsvReader reader = CsvUtil.getReader();
    33. List result = reader.read(ResourceUtil.getUtf8Reader(filePath), StrainEntity.class);
    34. long end = System.currentTimeMillis();
    35. System.out.println("hutool:"+ (end-start));
    36. System.out.println("行数:"+result.size());
    37. } catch (Exception e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. public void esayPoi(String filePath) {
    42. try {
    43. CsvImportParams params = new CsvImportParams(CsvImportParams.UTF8);
    44. long start = System.currentTimeMillis();
    45. List result = CsvImportUtil.importCsv(
    46. new FileInputStream(new File(filePath)),
    47. StrainEntity2.class,
    48. params);
    49. long end = System.currentTimeMillis();
    50. System.out.println("esayPoi:"+ (end-start));
    51. System.out.println("行数:"+result.size());
    52. } catch (Exception e) {
    53. e.printStackTrace();
    54. }
    55. }
    56. public void easyExcel1(String filePath) {
    57. try {
    58. ImportParams params = new ImportParams();
    59. List result = new ArrayList();
    60. long start = System.currentTimeMillis();
    61. EasyExcel.read(filePath, StrainEntity3.class, new AnalysisEventListener() {
    62. @Override
    63. public void invoke(StrainEntity3 reportExcel, AnalysisContext analysisContext) {
    64. // 将读取到的每一行存入reportDetails集合中
    65. result.add(reportExcel);
    66. }
    67. @Override
    68. public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
    69. }).excelType(ExcelTypeEnum.CSV).sheet().doRead();
    70. long end = System.currentTimeMillis();
    71. System.out.println("easyExcel1:"+ (end-start));
    72. System.out.println("行数:"+result.size());
    73. } catch (Exception e) {
    74. e.printStackTrace();
    75. }
    76. }
    77. public void easyExcel2(String filePath) {
    78. final Strain3ReadListener strain3ReadListener = new Strain3ReadListener() ;
    79. try {
    80. long start = System.currentTimeMillis();
    81. EasyExcel.read(filePath, StrainEntity3.class, strain3ReadListener).sheet().doRead();
    82. // EasyExcel.read(filePath)
    83. // .excelType(ExcelTypeEnum.CSV)
    84. // .registerReadListener(strain3ReadListener)
    85. // .sheet()
    86. // .doRead();
    87. List result = strain3ReadListener.getCachedDataList();
    88. long end = System.currentTimeMillis();
    89. System.out.println("easyExcel2:"+ (end-start));
    90. System.out.println("行数:"+result.size());
    91. } catch (Exception e) {
    92. e.printStackTrace();
    93. }
    94. }
    95. }

    3.3、读Excel

    1. package cn.sdjtky.ltpon.FileTest;
    2. import cn.afterturn.easypoi.excel.ExcelImportUtil;
    3. import cn.afterturn.easypoi.excel.entity.ImportParams;
    4. import cn.afterturn.easypoi.handler.inter.IReadHandler;
    5. import cn.hutool.poi.excel.ExcelReader;
    6. import cn.hutool.poi.excel.ExcelUtil;
    7. import cn.sdjtky.ltpon.easy.excel.listener.MergeCellsReadListener;
    8. import cn.sdjtky.ltpon.poi.DemoDataListener;
    9. import com.alibaba.excel.EasyExcel;
    10. import com.alibaba.excel.context.AnalysisContext;
    11. import com.alibaba.excel.enums.CellExtraTypeEnum;
    12. import com.alibaba.excel.event.AnalysisEventListener;
    13. import com.alibaba.excel.read.listener.PageReadListener;
    14. import com.alibaba.excel.support.ExcelTypeEnum;
    15. import com.alibaba.fastjson2.JSON;
    16. import org.junit.jupiter.api.Test;
    17. import org.slf4j.Logger;
    18. import org.slf4j.LoggerFactory;
    19. import java.io.File;
    20. import java.io.FileInputStream;
    21. import java.util.ArrayList;
    22. import java.util.List;
    23. public class ReadExcelTest {
    24. private static final Logger LOGGER = LoggerFactory.getLogger(ReadExcelTest.class);
    25. @Test
    26. public void test() {
    27. String filePath = "G:/test/1048575.xlsx";
    28. hutool(filePath);
    29. // esayPoi(filePath);
    30. // easyExcel1(filePath);
    31. // easyExcel2(filePath);
    32. }
    33. public void hutool(String filePath) {
    34. try {
    35. long start = System.currentTimeMillis();
    36. ExcelReader reader = ExcelUtil.getReader(filePath);
    37. List result = reader.readAll(StrainEntity.class);
    38. long end = System.currentTimeMillis();
    39. System.out.println("hutool:"+ (end-start));
    40. System.out.println("行数:"+result.size());
    41. } catch (Exception e) {
    42. e.printStackTrace();
    43. }
    44. }
    45. public void esayPoi(String filePath) {
    46. try {
    47. ImportParams params = new ImportParams();
    48. List result = new ArrayList();
    49. long start = System.currentTimeMillis();
    50. ExcelImportUtil.importExcelBySax(
    51. new FileInputStream(new File(filePath)),
    52. StrainEntity2.class,
    53. params,
    54. new IReadHandler() {
    55. @Override
    56. public void handler(StrainEntity2 o) {
    57. result.add(o);
    58. }
    59. @Override
    60. public void doAfterAll() {
    61. System.out.println("全部执行完毕了--------------------------------");
    62. }
    63. });
    64. long end = System.currentTimeMillis();
    65. System.out.println("esayPoi:"+ (end-start));
    66. System.out.println("行数:"+result.size());
    67. } catch (Exception e) {
    68. e.printStackTrace();
    69. }
    70. }
    71. public void easyExcel1(String filePath) {
    72. try {
    73. ImportParams params = new ImportParams();
    74. List result = new ArrayList();
    75. long start = System.currentTimeMillis();
    76. EasyExcel.read(filePath, StrainEntity3.class, new AnalysisEventListener() {
    77. @Override
    78. public void invoke(StrainEntity3 reportExcel, AnalysisContext analysisContext) {
    79. // 将读取到的每一行存入reportDetails集合中
    80. result.add(reportExcel);
    81. }
    82. @Override
    83. public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
    84. }).sheet().doRead();
    85. long end = System.currentTimeMillis();
    86. System.out.println("easyExcel1:"+ (end-start));
    87. System.out.println("行数:"+result.size());
    88. } catch (Exception e) {
    89. e.printStackTrace();
    90. }
    91. }
    92. public void easyExcel2(String filePath) {
    93. final Strain3ReadListener strain3ReadListener = new Strain3ReadListener() ;
    94. try {
    95. long start = System.currentTimeMillis();
    96. EasyExcel.read(filePath, StrainEntity3.class, strain3ReadListener).sheet().doRead();
    97. // EasyExcel.read(filePath)
    98. // .registerReadListener(strain3ReadListener)
    99. // .sheet()
    100. // .doRead();
    101. List result = strain3ReadListener.getCachedDataList();
    102. long end = System.currentTimeMillis();
    103. System.out.println("easyExcel2:"+ (end-start));
    104. System.out.println("行数:"+result.size());
    105. } catch (Exception e) {
    106. e.printStackTrace();
    107. }
    108. }
    109. }

    3.4、写CSV

    1. package cn.sdjtky.ltpon.FileTest;
    2. import cn.afterturn.easypoi.csv.CsvExportUtil;
    3. import cn.afterturn.easypoi.csv.CsvImportUtil;
    4. import cn.afterturn.easypoi.csv.entity.CsvExportParams;
    5. import cn.afterturn.easypoi.csv.entity.CsvImportParams;
    6. import cn.afterturn.easypoi.excel.entity.ImportParams;
    7. import cn.hutool.core.io.FastByteArrayOutputStream;
    8. import cn.hutool.core.io.resource.ResourceUtil;
    9. import cn.hutool.core.text.csv.CsvReader;
    10. import cn.hutool.core.text.csv.CsvUtil;
    11. import cn.hutool.core.text.csv.CsvWriter;
    12. import cn.hutool.core.util.CharsetUtil;
    13. import cn.sdjtky.ltpon.query.HumidityQuery;
    14. import cn.sdjtky.ltpon.utils.BeanCopierUtils;
    15. import com.alibaba.excel.EasyExcel;
    16. import com.alibaba.excel.ExcelWriter;
    17. import com.alibaba.excel.context.AnalysisContext;
    18. import com.alibaba.excel.event.AnalysisEventListener;
    19. import com.alibaba.excel.support.ExcelTypeEnum;
    20. import com.alibaba.excel.write.metadata.WriteSheet;
    21. import com.ruoyi.common.utils.uuid.IdUtils;
    22. import org.junit.jupiter.api.Test;
    23. import org.slf4j.Logger;
    24. import org.slf4j.LoggerFactory;
    25. import org.springframework.beans.BeanUtils;
    26. import java.io.ByteArrayInputStream;
    27. import java.io.File;
    28. import java.io.FileInputStream;
    29. import java.io.FileOutputStream;
    30. import java.util.ArrayList;
    31. import java.util.List;
    32. public class WriteCsvTest {
    33. private static final Logger LOGGER = LoggerFactory.getLogger(WriteCsvTest.class);
    34. @Test
    35. public void test() {
    36. String filePath = "G:/test/1048575.csv";
    37. List strainEntity3List = Rerad_easyExcel1(filePath);
    38. List strainEntityList = BeanCopierUtils.list(strainEntity3List, StrainEntity::new);
    39. List strainEntity2List = BeanCopierUtils.list(strainEntity3List, StrainEntity2::new);
    40. // List strainEntity3List = BeanCopierUtils.list(strainEntity3List, StrainEntity3::new);
    41. // hutool(strainEntityList);
    42. // esayPoi(strainEntity2List);
    43. // easyExcel1(strainEntity3List);
    44. easyExcel2(strainEntity3List);
    45. }
    46. public List Rerad_easyExcel1(String filePath) {
    47. List result = new ArrayList();
    48. try {
    49. ImportParams params = new ImportParams();
    50. EasyExcel.read(filePath, StrainEntity3.class, new AnalysisEventListener() {
    51. @Override
    52. public void invoke(StrainEntity3 reportExcel, AnalysisContext analysisContext) {
    53. // 将读取到的每一行存入reportDetails集合中
    54. result.add(reportExcel);
    55. }
    56. @Override
    57. public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
    58. }).excelType(ExcelTypeEnum.CSV).sheet().doRead();
    59. } catch (Exception e) {
    60. e.printStackTrace();
    61. }
    62. return result;
    63. }
    64. public void hutool(List list) {
    65. System.out.println("数据行数:"+list.size());
    66. try {
    67. long start = System.currentTimeMillis();
    68. CsvWriter writer = CsvUtil.getWriter("G:/test/write/hutool.csv", CharsetUtil.CHARSET_UTF_8);
    69. // writer.write(list);
    70. writer.writeBeans(list);
    71. long end = System.currentTimeMillis();
    72. long cost = end-start;
    73. System.out.println("hutool:"+ cost);
    74. } catch (Exception e) {
    75. e.printStackTrace();
    76. }
    77. }
    78. public void esayPoi(List list) {
    79. System.out.println("数据行数:"+list.size());
    80. try {
    81. long start = System.currentTimeMillis();
    82. CsvExportParams params = new CsvExportParams(CsvImportParams.UTF8);
    83. FileOutputStream fos = new FileOutputStream("G:/test/write/esayPoi.csv");
    84. CsvExportUtil.exportCsv(params, StrainEntity2.class, list, fos);
    85. fos.flush();
    86. fos.close();
    87. long end = System.currentTimeMillis();
    88. long cost = end-start;
    89. System.out.println("esayPoi:"+ cost);
    90. } catch (Exception e) {
    91. e.printStackTrace();
    92. }
    93. }
    94. public void easyExcel1(List list) {
    95. System.out.println("数据行数:"+list.size());
    96. try {
    97. long start = System.currentTimeMillis();
    98. FastByteArrayOutputStream fastByteArrayOutputStream = new FastByteArrayOutputStream();
    99. try {
    100. EasyExcel.write(fastByteArrayOutputStream, StrainEntity3.class)
    101. .autoCloseStream(false)
    102. .excelType(ExcelTypeEnum.CSV)
    103. .sheet("sheet页")
    104. .doWrite(list);
    105. FileOutputStream fileOutputStream = new FileOutputStream("G:/test/write/easyExcel.csv");
    106. fastByteArrayOutputStream.writeTo(fileOutputStream);
    107. fileOutputStream.flush();
    108. fileOutputStream.close();
    109. } finally {
    110. }
    111. long end = System.currentTimeMillis();
    112. long cost = end-start;
    113. System.out.println("easyExcel1:"+ cost);
    114. } catch (Exception e) {
    115. e.printStackTrace();
    116. }
    117. }
    118. public void easyExcel2(List list) {
    119. System.out.println("数据行数:"+list.size());
    120. try {
    121. long start = System.currentTimeMillis();
    122. try {
    123. EasyExcel.write("G:/test/write/easyExcel.csv", StrainEntity3.class).excelType(ExcelTypeEnum.CSV).sheet("sss").doWrite(list);
    124. } finally {
    125. }
    126. long end = System.currentTimeMillis();
    127. long cost = end-start;
    128. System.out.println("easyExcel2:"+ cost);
    129. } catch (Exception e) {
    130. e.printStackTrace();
    131. }
    132. }
    133. }

    3.5、写Excel

    1. package cn.sdjtky.ltpon.FileTest;
    2. import cn.afterturn.easypoi.excel.ExcelExportUtil;
    3. import cn.afterturn.easypoi.excel.ExcelImportUtil;
    4. import cn.afterturn.easypoi.excel.entity.ExportParams;
    5. import cn.afterturn.easypoi.excel.entity.ImportParams;
    6. import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
    7. import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
    8. import cn.afterturn.easypoi.handler.inter.IReadHandler;
    9. import cn.hutool.core.bean.BeanUtil;
    10. import cn.hutool.core.collection.CollUtil;
    11. import cn.hutool.core.io.FastByteArrayOutputStream;
    12. import cn.hutool.core.text.csv.CsvUtil;
    13. import cn.hutool.core.text.csv.CsvWriter;
    14. import cn.hutool.core.util.CharsetUtil;
    15. import cn.hutool.poi.excel.BigExcelWriter;
    16. import cn.hutool.poi.excel.ExcelReader;
    17. import cn.hutool.poi.excel.ExcelUtil;
    18. import cn.hutool.poi.excel.ExcelWriter;
    19. import cn.sdjtky.ltpon.utils.BeanCopierUtils;
    20. import com.alibaba.excel.EasyExcel;
    21. import com.alibaba.excel.context.AnalysisContext;
    22. import com.alibaba.excel.event.AnalysisEventListener;
    23. import com.alibaba.excel.support.ExcelTypeEnum;
    24. import com.alibaba.excel.write.metadata.WriteSheet;
    25. import com.alibaba.excel.write.metadata.fill.FillWrapper;
    26. import com.ruoyi.common.utils.uuid.IdUtils;
    27. import lombok.SneakyThrows;
    28. import org.apache.poi.ss.usermodel.Workbook;
    29. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
    30. import org.junit.jupiter.api.Test;
    31. import org.slf4j.Logger;
    32. import org.slf4j.LoggerFactory;
    33. import java.io.File;
    34. import java.io.FileInputStream;
    35. import java.io.FileOutputStream;
    36. import java.util.ArrayList;
    37. import java.util.HashMap;
    38. import java.util.List;
    39. import java.util.Map;
    40. public class WriteExcelTest {
    41. private static final Logger LOGGER = LoggerFactory.getLogger(WriteExcelTest.class);
    42. @Test
    43. public void test() {
    44. String filePath = "G:/test/1048575.csv";
    45. List strainEntity3List = Rerad_easyExcel1(filePath);
    46. List strainEntityList = BeanCopierUtils.list(strainEntity3List, StrainEntity::new);
    47. List strainEntity2List = BeanCopierUtils.list(strainEntity3List, StrainEntity2::new);
    48. // hutool1(strainEntityList);
    49. // hutool2(strainEntityList);
    50. // esayPoi1(strainEntity2List);
    51. // esayPoi2(strainEntity2List);
    52. // easyExcel1(strainEntity3List);
    53. // easyExcel2(strainEntity3List);
    54. // easyExcel3(strainEntity3List);
    55. easyExcel4(strainEntity3List);
    56. }
    57. public List Rerad_easyExcel1(String filePath) {
    58. List result = new ArrayList();
    59. try {
    60. ImportParams params = new ImportParams();
    61. EasyExcel.read(filePath, StrainEntity3.class, new AnalysisEventListener() {
    62. @Override
    63. public void invoke(StrainEntity3 reportExcel, AnalysisContext analysisContext) {
    64. // 将读取到的每一行存入reportDetails集合中
    65. result.add(reportExcel);
    66. }
    67. @Override
    68. public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
    69. }).excelType(ExcelTypeEnum.CSV).sheet().doRead();
    70. } catch (Exception e) {
    71. e.printStackTrace();
    72. }
    73. return result;
    74. }
    75. public void hutool1(List list) {
    76. System.out.println("数据行数:"+list.size());
    77. try {
    78. long start = System.currentTimeMillis();
    79. ExcelWriter writer = ExcelUtil.getWriter("G:/test/write/hutool.xlsx");
    80. writer.write(list, true);
    81. // 关闭writer,释放内存
    82. writer.close();
    83. long end = System.currentTimeMillis();
    84. long cost = end-start;
    85. System.out.println("hutool:"+ cost);
    86. } catch (Exception e) {
    87. e.printStackTrace();
    88. }
    89. }
    90. public void hutool2(List list) {
    91. System.out.println("数据行数:"+list.size());
    92. try {
    93. long start = System.currentTimeMillis();
    94. BigExcelWriter writer= ExcelUtil.getBigWriter("G:/test/write/hutool2"+ IdUtils.getSequenceID()+".xlsx");
    95. writer.write(list);
    96. writer.close();
    97. long end = System.currentTimeMillis();
    98. long cost = end-start;
    99. System.out.println("hutool2:"+ cost);
    100. } catch (Exception e) {
    101. e.printStackTrace();
    102. }
    103. }
    104. public void esayPoi1(List list) {
    105. System.out.println("数据行数:"+list.size());
    106. try {
    107. long start = System.currentTimeMillis();
    108. ExportParams params = new ExportParams();
    109. params.setMaxNum(1048576);
    110. Workbook workbook = null;
    111. workbook = ExcelExportUtil.exportBigExcel(params, StrainEntity2.class, new IExcelExportServer() {
    112. @Override
    113. public List selectListForExcelExport(Object obj, int page) {
    114. if (list.size()<=0){
    115. return null;
    116. }
    117. List listObj = new ArrayList();
    118. listObj.addAll(list);
    119. list.clear();
    120. return listObj;
    121. }
    122. }, 1);
    123. // workbook = ExcelExportUtil.exportExcel(params,StrainEntity2.class,list);
    124. FileOutputStream fos = new FileOutputStream("G:/test/write/esayPoi.xlsx");
    125. workbook.write(fos);
    126. fos.close();
    127. long end = System.currentTimeMillis();
    128. long cost = end-start;
    129. System.out.println("esayPoi:"+ cost);
    130. } catch (Exception e) {
    131. e.printStackTrace();
    132. }
    133. }
    134. public void esayPoi2(List list) {
    135. System.out.println("数据行数:"+list.size());
    136. try {
    137. long start = System.currentTimeMillis();
    138. Map map = new HashMap();
    139. map.put("list", list);
    140. TemplateExportParams params = new TemplateExportParams("G:/test/t/StrainEntity2.xlsx");
    141. Workbook workbook = ExcelExportUtil.exportExcel(params, map);
    142. FileOutputStream fos = new FileOutputStream("G:/test/write/esayPoi2.xlsx");
    143. workbook.write(fos);
    144. fos.close();
    145. long end = System.currentTimeMillis();
    146. long cost = end-start;
    147. System.out.println("esayPoi2:"+ cost);
    148. } catch (Exception e) {
    149. e.printStackTrace();
    150. }
    151. }
    152. public void easyExcel1(List list) {
    153. System.out.println("数据行数:"+list.size());
    154. try {
    155. long start = System.currentTimeMillis();
    156. FastByteArrayOutputStream fastByteArrayOutputStream = new FastByteArrayOutputStream();
    157. try {
    158. EasyExcel.write(fastByteArrayOutputStream, StrainEntity3.class)
    159. .autoCloseStream(false)
    160. .excelType(ExcelTypeEnum.XLSX)
    161. .sheet("sheet页")
    162. .doWrite(list);
    163. FileOutputStream fileOutputStream = new FileOutputStream("G:/test/write/easyExcel.xlsx");
    164. fastByteArrayOutputStream.writeTo(fileOutputStream);
    165. fileOutputStream.flush();
    166. fileOutputStream.close();
    167. } finally {
    168. }
    169. long end = System.currentTimeMillis();
    170. long cost = end-start;
    171. System.out.println("easyExcel1:"+ cost);
    172. } catch (Exception e) {
    173. e.printStackTrace();
    174. }
    175. }
    176. public void easyExcel2(List list) {
    177. System.out.println("数据行数:"+list.size());
    178. try {
    179. long start = System.currentTimeMillis();
    180. try {
    181. EasyExcel.write("G:/test/write/easyExcel.xlsx", StrainEntity3.class).excelType(ExcelTypeEnum.XLSX).sheet("sss").doWrite(list);
    182. } finally {
    183. }
    184. long end = System.currentTimeMillis();
    185. long cost = end-start;
    186. System.out.println("easyExcel2:"+ cost);
    187. } catch (Exception e) {
    188. e.printStackTrace();
    189. }
    190. }
    191. public void easyExcel3(List list) {
    192. System.out.println("数据行数:"+list.size());
    193. try {
    194. long start = System.currentTimeMillis();
    195. String templateFileName = "G:/test/t/StrainEntity3.xlsx";
    196. String fileName = "G:/test/write/easyExcel.xlsx";
    197. // 方案1 一下子全部放到内存里面 并填充
    198. // 这里 会填充到第一个sheet, 然后文件流会自动关闭
    199. EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(list);
    200. long end = System.currentTimeMillis();
    201. long cost = end-start;
    202. System.out.println("easyExcel3:"+ cost);
    203. } catch (Exception e) {
    204. e.printStackTrace();
    205. }
    206. }
    207. public void easyExcel4(List list) {
    208. System.out.println("数据行数:"+list.size());
    209. try {
    210. long start = System.currentTimeMillis();
    211. FastByteArrayOutputStream fastByteArrayOutputStream = new FastByteArrayOutputStream();
    212. // 方案2 分多次 填充 会使用文件缓存(省内存)
    213. String templateFileName = "G:/test/t/StrainEntity3.xlsx";
    214. String fileName = "G:/test/write/easyExcel.xlsx";
    215. try (com.alibaba.excel.ExcelWriter excelWriter = EasyExcel.write(fastByteArrayOutputStream).withTemplate(templateFileName).build()) {
    216. WriteSheet writeSheet = EasyExcel.writerSheet().build();
    217. excelWriter.fill(list, writeSheet);
    218. }
    219. // String templateFileName = "G:/test/t/StrainEntity3.1.xlsx";
    220. // String fileName = "G:/test/write/easyExcel.xlsx";
    221. // try (com.alibaba.excel.ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
    222. // WriteSheet writeSheet = EasyExcel.writerSheet().build();
    223. // excelWriter.fill(new FillWrapper("sl", list), writeSheet);
    224. // }
    225. // String templateFileName = "G:/test/t/StrainEntity3.1.xlsx";
    226. // String fileName = "G:/test/write/easyExcel.xlsx";
    227. // try (com.alibaba.excel.ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
    228. // WriteSheet writeSheet = EasyExcel.writerSheet().build();
    229. // excelWriter.fill(new FillWrapper("sl", listX), writeSheet);
    230. // excelWriter.fill(new FillWrapper("sl", listX), writeSheet);
    231. // excelWriter.fill(new FillWrapper("sl", listX), writeSheet);
    232. // excelWriter.fill(new FillWrapper("sl", listX), writeSheet);
    233. // excelWriter.fill(new FillWrapper("sl", listX), writeSheet);
    234. // }
    235. long end = System.currentTimeMillis();
    236. long cost = end-start;
    237. System.out.println("easyExcel4:"+ cost);
    238. } catch (Exception e) {
    239. e.printStackTrace();
    240. }
    241. }
    242. }
    243. 3.6、业务示例

      1. package cn.sdjtky.ltpon.FileTest;
      2. import cn.afterturn.easypoi.excel.ExcelExportUtil;
      3. import cn.afterturn.easypoi.excel.entity.ExportParams;
      4. import cn.afterturn.easypoi.excel.entity.ImportParams;
      5. import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
      6. import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
      7. import cn.hutool.core.io.FastByteArrayOutputStream;
      8. import cn.hutool.poi.excel.BigExcelWriter;
      9. import cn.hutool.poi.excel.ExcelUtil;
      10. import cn.hutool.poi.excel.ExcelWriter;
      11. import cn.sdjtky.ltpon.utils.BeanCopierUtils;
      12. import cn.sdjtky.ltpon.utils.LocalDateTimeUtils;
      13. import com.alibaba.excel.EasyExcel;
      14. import com.alibaba.excel.context.AnalysisContext;
      15. import com.alibaba.excel.enums.WriteDirectionEnum;
      16. import com.alibaba.excel.event.AnalysisEventListener;
      17. import com.alibaba.excel.support.ExcelTypeEnum;
      18. import com.alibaba.excel.write.metadata.WriteSheet;
      19. import com.alibaba.excel.write.metadata.fill.FillConfig;
      20. import com.alibaba.excel.write.metadata.fill.FillWrapper;
      21. import com.ruoyi.common.utils.DateUtils;
      22. import com.ruoyi.common.utils.uuid.IdUtils;
      23. import org.apache.poi.ss.usermodel.Workbook;
      24. import org.junit.jupiter.api.Test;
      25. import org.slf4j.Logger;
      26. import org.slf4j.LoggerFactory;
      27. import org.springframework.core.io.DefaultResourceLoader;
      28. import org.springframework.core.io.Resource;
      29. import java.io.ByteArrayOutputStream;
      30. import java.io.FileOutputStream;
      31. import java.time.LocalDateTime;
      32. import java.util.*;
      33. public class StressStrainTest {
      34. private static final Logger LOGGER = LoggerFactory.getLogger(StressStrainTest.class);
      35. private static final String stationName = "临沂G205观测点";
      36. private static final String pileNumber = "K103--K113";
      37. private static final String observer = "徐本锡";
      38. private static final String observationDate = LocalDateTimeUtils.format(LocalDateTimeUtils.DEFAULT_PATTERN,LocalDateTime.now());
      39. private static final String reviewer = "金小少";
      40. private static final String reviewDate = LocalDateTimeUtils.format(LocalDateTimeUtils.DEFAULT_PATTERN,LocalDateTime.now());
      41. @Test
      42. public void test() {
      43. // esayPoi2();
      44. easyExcel4();
      45. // easyExcelCSV2();
      46. }
      47. public Map getData(){
      48. Map map = new HashMap();
      49. List deviceList = new ArrayList();
      50. for (int i=1; i<=8; i++){
      51. DeviceDto deviceDto= new DeviceDto();
      52. deviceDto.setSn("传感器x_"+i);
      53. deviceDto.setSurfaceDistance(Double.valueOf(i));
      54. deviceDto.setStructuralLayer("层位"+i);
      55. deviceDto.setChannel("电信号");
      56. deviceDto.setDataKey("t.v"+i);
      57. deviceList.add(deviceDto);
      58. }
      59. List> dataList = new ArrayList>();
      60. List dataList2 = new ArrayList();
      61. for(int i=0; i<100; i++){
      62. Map m = new HashMap();
      63. StressStrainDataDto dto = new StressStrainDataDto();
      64. for (int j=1; j<=8; j++){
      65. m.put("date", DateUtils.datePath());
      66. m.put("time", DateUtils.dateTimeNow(DateUtils.HHMMssSSS_1));
      67. m.put("v"+j, Math.random()+1);
      68. dto.setDate(DateUtils.datePath());
      69. dto.setTime(DateUtils.dateTimeNow(DateUtils.HHMMssSSS_1));
      70. dto.setDateTime(DateUtils.dateTimeNow(DateUtils.YYYYMMDDHHMMssSSS_1));
      71. dto.setV1( Math.random()+1);
      72. dto.setV2( Math.random()+1);
      73. dto.setV3( Math.random()+1);
      74. dto.setV4( Math.random()+1);
      75. dto.setV5( Math.random()+1);
      76. dto.setV6( Math.random()+1);
      77. dto.setV7( Math.random()+1);
      78. dto.setV8( Math.random()+1);
      79. }
      80. dataList.add(m);
      81. dataList2.add(dto);
      82. }
      83. map.put("stationName", stationName);
      84. map.put("pileNumber", pileNumber);
      85. map.put("observer", observer);
      86. map.put("observationDate", observationDate);
      87. map.put("reviewer", reviewer);
      88. map.put("reviewDate", reviewDate);
      89. map.put("deviceList", deviceList);
      90. map.put("dataList", dataList2);
      91. return map;
      92. }
      93. public void esayPoi2() {
      94. Map data = getData();
      95. System.out.println("数据行数:"+((List)data.get("dataList")).size());
      96. try {
      97. long start = System.currentTimeMillis();
      98. TemplateExportParams params = new TemplateExportParams("G:/test/t/stressStrainTemplateDongHua.xlsx");
      99. params.setColForEach(true);
      100. Workbook workbook = ExcelExportUtil.exportExcel(params, data);
      101. FileOutputStream fos = new FileOutputStream("G:/test/write/stressStrainTemplateDongHua.xlsx");
      102. workbook.write(fos);
      103. fos.close();
      104. long end = System.currentTimeMillis();
      105. long cost = end-start;
      106. System.out.println("esayPoi2:"+ cost);
      107. } catch (Exception e) {
      108. e.printStackTrace();
      109. }
      110. }
      111. public void easyExcel4() {
      112. Map data = getData();
      113. Map data1 = new HashMap();
      114. data1.put("stationName", data.get("stationName"));
      115. data1.put("pileNumber", data.get("pileNumber"));
      116. data1.put("observer", data.get("observer"));
      117. data1.put("observationDate", data.get("observationDate"));
      118. data1.put("reviewer", data.get("reviewer"));
      119. data1.put("reviewDate", data.get("reviewDate"));
      120. List deviceList = (List) data.get("deviceList");
      121. List dataList = (List) data.get("dataList");
      122. // 根据用户传入字段 导出数据
      123. Set includeColumnFiledNames = new HashSet();
      124. includeColumnFiledNames.add("date");
      125. // includeColumnFiledNames.add("time");
      126. for(int i=0; i
      127. includeColumnFiledNames.add("v"+(i+1));
      128. }
      129. System.out.println("数据行数:"+((List)data.get("dataList")).size());
      130. try {
      131. long start = System.currentTimeMillis();
      132. Resource resource = new DefaultResourceLoader().getResource("classpath:templates/stressStrain/stressStrainTemplateDongHua.xlsx");
      133. FastByteArrayOutputStream outputStream = new FastByteArrayOutputStream();
      134. // 方案2 分多次 填充 会使用文件缓存(省内存)
      135. // String templateFileName = "G:/test/t/stressStrainTemplateDongHua2.xlsx";
      136. // try (com.alibaba.excel.ExcelWriter excelWriter = EasyExcel.write(fastByteArrayOutputStream).withTemplate(templateFileName).build()) {
      137. // WriteSheet writeSheet = EasyExcel.writerSheet().build();
      138. // FillConfig fillConfig = FillConfig.builder().autoStyle(true).direction(WriteDirectionEnum.HORIZONTAL).build();
      139. // excelWriter.fill(data1, writeSheet);
      140. // excelWriter.fill(new FillWrapper("deviceList", deviceList), fillConfig, writeSheet);
      141. writeSheet.setIncludeColumnFieldNames(includeColumnFiledNames);
      142. // excelWriter.fill(new FillWrapper("dataList", dataList), writeSheet);
      143. // }
      144. try (com.alibaba.excel.ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(resource.getInputStream()).build()) {
      145. WriteSheet writeSheet = EasyExcel.writerSheet().build();
      146. FillConfig fillConfig = FillConfig.builder().autoStyle(true).direction(WriteDirectionEnum.HORIZONTAL).build();
      147. excelWriter.fill(data1, writeSheet);
      148. excelWriter.fill(new FillWrapper("deviceList", deviceList), fillConfig, writeSheet);
      149. excelWriter.fill(new FillWrapper("dataList", dataList), writeSheet);
      150. excelWriter.close();
      151. outputStream.flush();
      152. outputStream.close();
      153. }
      154. // FileOutputStream fileOutputStream = new FileOutputStream("G:/test/write/123.xlsx");
      155. // outputStream.writeTo(fileOutputStream);
      156. // fileOutputStream.flush();
      157. // fileOutputStream.close();
      158. long end = System.currentTimeMillis();
      159. // String templateFileName = "G:/test/t/stressStrainTemplateDongHua2.xlsx";
      160. // String fileName = "G:/test/write/stressStrainTemplateDongHua2.xlsx";
      161. // try (com.alibaba.excel.ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(resource.getInputStream()).build()) {
      162. // WriteSheet writeSheet = EasyExcel.writerSheet().build();
      163. // FillConfig fillConfig = FillConfig.builder().autoStyle(true).direction(WriteDirectionEnum.HORIZONTAL).build();
      164. // excelWriter.fill(data1, writeSheet);
      165. // excelWriter.fill(new FillWrapper("deviceList", deviceList), fillConfig, writeSheet);
      166. writeSheet.setIncludeColumnFieldNames(includeColumnFiledNames);
      167. // excelWriter.fill(new FillWrapper("dataList", dataList), writeSheet);
      168. // }
      169. long cost = end-start;
      170. System.out.println("easyExcel4:"+ cost);
      171. } catch (Exception e) {
      172. e.printStackTrace();
      173. }
      174. }
      175. public void easyExcelCSV2() {
      176. Map data = getData();
      177. List deviceList = (List) data.get("deviceList");
      178. List dataList = (List) data.get("dataList");
      179. // 根据用户传入字段 导出数据
      180. Set includeColumnFiledNames = new HashSet();
      181. List> headList = new ArrayList>();
      182. List head0 = new ArrayList();
      183. head0.add("时间");
      184. head0.add("与路表距离");
      185. head0.add("结构层位");
      186. headList.add(head0);
      187. includeColumnFiledNames.add("dateTime");
      188. for(int i=0; i
      189. includeColumnFiledNames.add("v"+(i+1));
      190. List head = new ArrayList();
      191. head.add(deviceList.get(i).getSn());
      192. head.add(deviceList.get(i).getSurfaceDistance().toString());
      193. head.add(deviceList.get(i).getStructuralLayer());
      194. headList.add(head);
      195. }
      196. System.out.println("数据行数:"+((List)data.get("dataList")).size());
      197. try {
      198. long start = System.currentTimeMillis();
      199. try {
      200. EasyExcel.write("G:/test/write/stressStrainTemplateDongHua2.csv", StressStrainDataDto.class).head(headList).includeColumnFieldNames(includeColumnFiledNames).excelType(ExcelTypeEnum.CSV).sheet("sss").doWrite(dataList);
      201. } finally {
      202. }
      203. long end = System.currentTimeMillis();
      204. long cost = end-start;
      205. System.out.println("easyExcel2:"+ cost);
      206. } catch (Exception e) {
      207. e.printStackTrace();
      208. }
      209. }
      210. }

    244. 相关阅读:
      [spark] RDD 编程指南(翻译)
      [2023.09.25]:Rust编写基于web_sys的编辑器:输入光标再次定位的小结
      一篇让你使用vue-cli搭建SPA项目
      【Spring Web教程】SpringBoot 实现一应用多Tomcat容器
      终于有人把不同标签的加工内容与落库讲明白了丨DTVision分析洞察篇
      【博客551】实现主备高可用vip的几种方式
      关于地方美食的HTML网页设计——地方美食介绍网站 HTML顺德美食介绍 html网页制作代码大全
      从零开始运行YOLOV5
      Win11中Yolo V10安装过程记录
      实时即未来,车联网项目之电子围栏分析【六】
    245. 原文地址:https://blog.csdn.net/xubenxismile/article/details/136375721