一、背景

为什么会用Easyexcel来做Excel上传

平时项目中经常使用EasyExcel从本地读取Excel中的数据,还有一个前端页面对需要处理的数据进行一些配置(如:Excel所在的文件夹,Excel的文件名,以及Sheet列名、处理数据需要的某些参数),由于每次都是读取的本地的文件,我就在想,如果某一天需要通过前端上传excel给我,让我来进行处理我又应该怎么办呢?我怎么才能在尽量少修改代码的前提下实现这个功能呢(由于公司经常改需求,项目已经重新写了3次了)?后来查了很多资料,发现Excel可以使用InPutStream流来读取Excel,我就突然明白了什么。

阿里巴巴语雀团队对EasyExcel是这样介绍的
  1. Java解析、生成Excel比较有名的框架有Apache poijxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi07Excel的解析,能够原本一个3MexcelPOI sax依然需要100M左右内存降低到几M,并且再大的excel不会出现内存溢出,03版依赖POIsax模式。在上层做了模型转换的封装,让使用者更加简单方便。
    当然还有急速模式能更快,但是内存占用会在100M多一点。

二、集成EasyExcel

隐藏内容,评论后阅读
评论后,请刷新页面

 

6、测试

我们先搞一个简单的Excel,用来测试

 

然后通过Postman模拟发送请求

  • 选择Post请求并输入请求地址
  • 在下面选择Body
  • Key的框中输入controller中的请求的方法中的参数,后面的下拉框中选择File
  • VALUE框中有一个Select File ,点击后选择自己刚才创建的测试的Excel
  • 最后点击Send发送请求

返回值如下:

由于我读了两次都放在同一个List中返回,所以返回值中有8个对象。

  1. [
  2. {
  3. "name": "小黑",
  4. "age": 25,
  5. "id": 1
  6. },
  7. {
  8. "name": "小白",
  9. "age": 22,
  10. "id": 2
  11. },
  12. {
  13. "name": "小黄",
  14. "age": 22,
  15. "id": 3
  16. },
  17. {
  18. "name": "小绿",
  19. "age": 23,
  20. "id": 4
  21. },
  22. {
  23. "name": "小黑",
  24. "age": 25,
  25. "id": 1
  26. },
  27. {
  28. "name": "小白",
  29. "age": 22,
  30. "id": 2
  31. },
  32. {
  33. "name": "小黄",
  34. "age": 22,
  35. "id": 3
  36. },
  37. {
  38. "name": "小绿",
  39. "age": 23,
  40. "id": 4
  41. }
  42. ]

三、EasyExcel中的Read方法汇总

  1. /**
  2. * Build excel the read
  3. *
  4. * @return Excel reader builder.
  5. */
  6. public static ExcelReaderBuilder read() {
  7. return new ExcelReaderBuilder();
  8. }
  9. /**
  10. * Build excel the read
  11. *
  12. * @param file
  13. * File to read.
  14. * @return Excel reader builder.
  15. */
  16. public static ExcelReaderBuilder read(File file) {
  17. return read(file, null, null);
  18. }
  19. /**
  20. * Build excel the read
  21. *
  22. * @param file
  23. * File to read.
  24. * @param readListener
  25. * Read listener.
  26. * @return Excel reader builder.
  27. */
  28. public static ExcelReaderBuilder read(File file, ReadListener readListener) {
  29. return read(file, null, readListener);
  30. }
  31. /**
  32. * Build excel the read
  33. *
  34. * @param file
  35. * File to read.
  36. * @param head
  37. * Annotate the class for configuration information.
  38. * @param readListener
  39. * Read listener.
  40. * @return Excel reader builder.
  41. */
  42. public static ExcelReaderBuilder read(File file, Class head, ReadListener readListener) {
  43. ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder();
  44. excelReaderBuilder.file(file);
  45. if (head != null) {
  46. excelReaderBuilder.head(head);
  47. }
  48. if (readListener != null) {
  49. excelReaderBuilder.registerReadListener(readListener);
  50. }
  51. return excelReaderBuilder;
  52. }
  53. /**
  54. * Build excel the read
  55. *
  56. * @param pathName
  57. * File path to read.
  58. * @return Excel reader builder.
  59. */
  60. public static ExcelReaderBuilder read(String pathName) {
  61. return read(pathName, null, null);
  62. }
  63. /**
  64. * Build excel the read
  65. *
  66. * @param pathName
  67. * File path to read.
  68. * @param readListener
  69. * Read listener.
  70. * @return Excel reader builder.
  71. */
  72. public static ExcelReaderBuilder read(String pathName, ReadListener readListener) {
  73. return read(pathName, null, readListener);
  74. }
  75. /**
  76. * Build excel the read
  77. *
  78. * @param pathName
  79. * File path to read.
  80. * @param head
  81. * Annotate the class for configuration information.
  82. * @param readListener
  83. * Read listener.
  84. * @return Excel reader builder.
  85. */
  86. public static ExcelReaderBuilder read(String pathName, Class head, ReadListener readListener) {
  87. ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder();
  88. excelReaderBuilder.file(pathName);
  89. if (head != null) {
  90. excelReaderBuilder.head(head);
  91. }
  92. if (readListener != null) {
  93. excelReaderBuilder.registerReadListener(readListener);
  94. }
  95. return excelReaderBuilder;
  96. }
  97. /**
  98. * Build excel the read
  99. *
  100. * @param inputStream
  101. * Input stream to read.
  102. * @return Excel reader builder.
  103. */
  104. public static ExcelReaderBuilder read(InputStream inputStream) {
  105. return read(inputStream, null, null);
  106. }
  107. /**
  108. * Build excel the read
  109. *
  110. * @param inputStream
  111. * Input stream to read.
  112. * @param readListener
  113. * Read listener.
  114. * @return Excel reader builder.
  115. */
  116. public static ExcelReaderBuilder read(InputStream inputStream, ReadListener readListener) {
  117. return read(inputStream, null, readListener);
  118. }
  119. /**
  120. * Build excel the read
  121. *
  122. * @param inputStream
  123. * Input stream to read.
  124. * @param head
  125. * Annotate the class for configuration information.
  126. * @param readListener
  127. * Read listener.
  128. * @return Excel reader builder.
  129. */
  130. public static ExcelReaderBuilder read(InputStream inputStream, Class head, ReadListener readListener) {
  131. ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder();
  132. excelReaderBuilder.file(inputStream);
  133. if (head != null) {
  134. excelReaderBuilder.head(head);
  135. }
  136. if (readListener != null) {
  137. excelReaderBuilder.registerReadListener(readListener);
  138. }
  139. return excelReaderBuilder;
  140. }

所有的方法都在这儿了,其实如果看不懂到底应该调用哪一个read方法的话,可以以根据自己所能得到的参数来判断。

四、扩展

  • 读取本地Excel
    1. public static void main(String[] args) {
         EasyExcel.read(“C:/Users/Lonely Programmer/Desktop/新建 Microsoft Excel 工作表.xlsx”
                        ,ExcelEntity.class
                        ,new UploadExcelListener())
            .doReadAll();
      }

    读取本地的Excel和通过InPutStream流读取的方式是一样的,只是参数变了,原本传的是InPutStream流,现在传的是文件的绝对路径。

  • MultipartFile文档

    MultipartFile文档地址:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html

    翻译是通过Google Chrome自带翻译插件进行翻译的,建议大家使用Google Chrome打开,自带翻译功能

 

平时上班比较忙,操作过程中如果遇到什么问题,可以评论提问,我看到了的话,有时间基本上都会回复的(前提是我会,哈哈哈)。

其实百度才是最好的老师。