当前位置:   article > 正文

Java使用EasyExcel实现批量导入_excel批量导入java easyexcel

excel批量导入java easyexcel

EasyExcel是阿里下的开源产品,基于poi进行的开发,相比于其他的解析方式不要太6。接下来就是使用的方式

第一步:引入jar包

  1. <!-- excel工具框架 -->
  2. <dependency>
  3. <groupId>org.apache.poi</groupId>
  4. <artifactId>poi-ooxml</artifactId>
  5. <version>3.15</version>
  6. </dependency>
  7. <!--easyexcel解析excell-->
  8. <dependency>
  9. <groupId>com.alibaba</groupId>
  10. <artifactId>easyexcel</artifactId>
  11. <version>1.0.3</version>
  12. </dependency>

第二步:读取工具类封装

  1. package com.edu.common.utils;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.PushbackInputStream;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import com.alibaba.excel.metadata.BaseRowModel;
  8. import com.alibaba.excel.metadata.Sheet;
  9. import org.apache.poi.EmptyFileException;
  10. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  11. import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
  12. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  13. import org.apache.poi.util.IOUtils;
  14. import com.alibaba.excel.ExcelReader;
  15. import com.alibaba.excel.read.context.AnalysisContext;
  16. import com.alibaba.excel.read.event.AnalysisEventListener;
  17. import com.alibaba.excel.support.ExcelTypeEnum;
  18. /**
  19. *@ClassName EasyExcelUtil
  20. *@Description easyExcel操作excel工具类
  21. *@Auther William
  22. *@Date 2019/6/5 11:35
  23. *@Version 1.0
  24. */
  25. public class EasyExcelUtil {
  26. /**
  27. * @param in 文件输入流
  28. * @param customContent
  29. * 自定义模型可以在
  30. * AnalysisContext中获取用于监听者回调使用
  31. * @param eventListener 用户监听
  32. * @throws IOException
  33. * @throws EmptyFileException
  34. * @throws InvalidFormatException
  35. */
  36. public static ExcelReader getExcelReader(InputStream in, Object customContent,AnalysisEventListener<?> eventListener) throws EmptyFileException, IOException, InvalidFormatException {
  37. // 如果输入流不支持mark/reset,需要对其进行包裹
  38. if (!in.markSupported()) {
  39. in = new PushbackInputStream(in, 8);
  40. }
  41. // 确保至少有一些数据
  42. byte[] header8 = IOUtils.peekFirst8Bytes(in);
  43. ExcelTypeEnum excelTypeEnum = null;
  44. if (NPOIFSFileSystem.hasPOIFSHeader(header8)) {
  45. excelTypeEnum = ExcelTypeEnum.XLS;
  46. }
  47. if (DocumentFactoryHelper.hasOOXMLHeader(in)) {
  48. excelTypeEnum = ExcelTypeEnum.XLSX;
  49. }
  50. if (excelTypeEnum != null) {
  51. return new ExcelReader(in, excelTypeEnum, customContent, eventListener);
  52. }
  53. throw new InvalidFormatException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
  54. }
  55. /**
  56. * @param in 文件输入流
  57. * @param customContent
  58. * 自定义模型可以在
  59. * AnalysisContext中获取用于监听者回调使用
  60. * @param eventListener 用户监听
  61. * @param trim 是否对解析的String做trim()默认true,用于防止 excel中空格引起的装换报错。
  62. * @throws IOException
  63. * @throws EmptyFileException
  64. * @throws InvalidFormatException
  65. */
  66. public static ExcelReader getExcelReader(InputStream in, Object customContent,
  67. AnalysisEventListener<?> eventListener, boolean trim)
  68. throws EmptyFileException, IOException, InvalidFormatException {
  69. // 如果输入流不支持mark/reset,需要对其进行包裹
  70. if (!in.markSupported()) {
  71. in = new PushbackInputStream(in, 8);
  72. }
  73. // 确保至少有一些数据
  74. byte[] header8 = IOUtils.peekFirst8Bytes(in);
  75. ExcelTypeEnum excelTypeEnum = null;
  76. if (NPOIFSFileSystem.hasPOIFSHeader(header8)) {
  77. excelTypeEnum = ExcelTypeEnum.XLS;
  78. }
  79. if (DocumentFactoryHelper.hasOOXMLHeader(in)) {
  80. excelTypeEnum = ExcelTypeEnum.XLSX;
  81. }
  82. if (excelTypeEnum != null) {
  83. return new ExcelReader(in, excelTypeEnum, customContent, eventListener, trim);
  84. }
  85. throw new InvalidFormatException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
  86. }
  87. /*
  88. *@Description: 读取Excel文件内容
  89. *@param in excel文件流
  90. *@param tClass 对应excel实体bean
  91. *@return: 对应excel实体bean的list
  92. *@Author: William
  93. *@Date: 2019/6/5 13:24
  94. */
  95. public static<T> List<T> getExcelContent(InputStream in, Class<T> tClass){
  96. List<T> excelPropertyIndexModelList = new ArrayList<>();
  97. try {
  98. AnalysisEventListener<T> listener = new AnalysisEventListener<T>() {
  99. @Override
  100. public void invoke(T excelPropertyIndexModel, AnalysisContext analysisContext) {
  101. excelPropertyIndexModelList.add(excelPropertyIndexModel);
  102. }
  103. @Override
  104. public void doAfterAllAnalysed(AnalysisContext analysisContext) {
  105. //读取之后的操作
  106. }
  107. };
  108. ExcelReader excelReader = EasyExcelUtil.getExcelReader(in, null, listener);
  109. // 第二个参数为表头行数,按照实际设置
  110. excelReader.read(new Sheet(1, 1, (Class<? extends BaseRowModel>) tClass));
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. }
  114. return excelPropertyIndexModelList;
  115. }
  116. }

第三步:封装excel对应的实体bean(这一步要注意,每一个字段跟表格要对应起来)

  1. package com.edu.common.pojo;
  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import java.util.List;
  5. import com.alibaba.excel.annotation.ExcelProperty;
  6. import com.alibaba.excel.metadata.BaseRowModel;
  7. import com.edu.common.utils.EasyExcelUtil;
  8. /**
  9. *@ClassName ExcelImportUserInfo
  10. *@Description 批量导入用户信息模板bean
  11. *@Auther William
  12. *@Date 2019/6/5 13:32
  13. *@Version 1.0
  14. */
  15. public class ExcelImportUserInfo extends BaseRowModel{
  16. @ExcelProperty(value = "用户名", index = 0)
  17. private String userName;
  18. @ExcelProperty(value = "用户角色", index = 1)
  19. private String userRole;
  20. @ExcelProperty(value = "用户姓名", index = 2)
  21. private String userId;
  22. @ExcelProperty(value = "用户密码", index = 3)
  23. private String password;
  24. @ExcelProperty(value = "性别", index = 4)
  25. private String sex;
  26. @ExcelProperty(value = "用户年级", index = 5)
  27. private String gradeId;
  28. @ExcelProperty(value = "用户学校", index = 6)
  29. private String schoolId;
  30. @ExcelProperty(value = "用户状态", index = 7)
  31. private String userStatus;
  32. public String getUserName() {
  33. return userName;
  34. }
  35. public void setUserName(String userName) {
  36. this.userName = userName;
  37. }
  38. public String getUserRole() {
  39. return userRole;
  40. }
  41. public void setUserRole(String userRole) {
  42. this.userRole = userRole;
  43. }
  44. public String getUserId() {
  45. return userId;
  46. }
  47. public void setUserId(String userId) {
  48. this.userId = userId;
  49. }
  50. public String getPassword() {
  51. return password;
  52. }
  53. public void setPassword(String password) {
  54. this.password = password;
  55. }
  56. public String getSex() {
  57. return sex;
  58. }
  59. public void setSex(String sex) {
  60. this.sex = sex;
  61. }
  62. public String getGradeId() {
  63. return gradeId;
  64. }
  65. public void setGradeId(String gradeId) {
  66. this.gradeId = gradeId;
  67. }
  68. public String getSchoolId() {
  69. return schoolId;
  70. }
  71. public void setSchoolId(String schoolId) {
  72. this.schoolId = schoolId;
  73. }
  74. public String getUserStatus() {
  75. return userStatus;
  76. }
  77. public void setUserStatus(String userStatus) {
  78. this.userStatus = userStatus;
  79. }
  80. @Override
  81. public String toString() {
  82. return "ExcelImportUserInfo{" +
  83. "userName='" + userName + '\'' +
  84. ", userRole='" + userRole + '\'' +
  85. ", userId='" + userId + '\'' +
  86. ", password='" + password + '\'' +
  87. ", sex='" + sex + '\'' +
  88. ", gradeId='" + gradeId + '\'' +
  89. ", schoolId='" + schoolId + '\'' +
  90. ", userStatus='" + userStatus + '\'' +
  91. '}';
  92. }
  93. }

对应的excel表格

测试方法:(测试方法基于jdk1.8,如果1.7自己换成for循环吧)

public static void main(String[] args) throws Exception {

InputStream in = new FileInputStream("E://aaa.xlsx");
  //读取的工具类可以根据自己需要封装
  //我这边是需要List集合所以封装了集合

List<ExcelImportUserInfo> excelContentList = EasyExcelUtil.getExcelContent(in, ExcelImportUserInfo.class); excelContentList.forEach(excelPropertyIndexModel -> {

           System.out.println(excelPropertyIndexModel.toString());

});

}

好了,以上就是使用方法了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/496073
推荐阅读
相关标签
  

闽ICP备14008679号