SpringBoot中EasyExcel如何实现Excel文件的导入导出

发布时间:2020-10-27 22:53:38 作者:Leah
来源:亿速云 阅读:415

这篇文章运用简单易懂的例子给大家介绍SpringBoot中EasyExcel如何实现Excel文件的导入导出,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

Easyexcel

Easyexcel 是阿里巴巴的开源项目,用来优化Excel文件处理过程:

SpringBoot中EasyExcel如何实现Excel文件的导入导出

SpringBoot+ EasyExcel实现Excel文件的导入导出

导入依赖

<!--lombok-->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.2</version>
  <optional>true</optional>
</dependency>

<!--easyExcel-->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>easyexcel</artifactId>
  <version>1.1.2-beat1</version>
</dependency>

<!--fastjson-->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <exclusions>
    <exclusion>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
</dependency>

为了防止Excel文件被破坏在pom.xml添加以下内容

<build>
  <plugins>
    <!-- 让maven不编译xls文件,但仍将其打包 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <configuration>
        <nonFilteredFileExtensions>
          <nonFilteredFileExtension>xls</nonFilteredFileExtension>
          <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
      </configuration>
    </plugin>
  </plugins>
</build>

application.propertis:配置文件

#temp files
project.tmp.files.path=/Users/mac/Desktop/image/tmp/files/

在SpringBoot启动类添加临时文件设置

@Value("${project.tmp.files.path}")
public String filesPath;

@Bean
MultipartConfigElement multipartConfigElement() {
  MultipartConfigFactory factory = new MultipartConfigFactory();
  //设置路径xxx
  factory.setLocation(filesPath);
  return factory.createMultipartConfig();
}

ExcelUtil:Excel工具类

@Slf4j
public class ExcelUtil {
  private static Sheet initSheet;


  static {
    initSheet = new Sheet(1, 0);
    initSheet.setSheetName("sheet");
    //设置自适应宽度
    initSheet.setAutoWidth(Boolean.TRUE);
  }

  public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
    try {
      response.setCharacterEncoding("UTF-8");
      response.setContentType("application/octet-stream;charset=utf-8");
      response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
      workbook.write(response.getOutputStream());
    } catch (IOException e) {
      // throw new NormalException(e.getMessage());
    }
  }


  /**
   * 读取少于1000行数据
   *
   * @param filePath 文件绝对路径
   * @return
   */
  public static List<Object> readLessThan1000Row(String filePath) {
    return readLessThan1000RowBySheet(filePath, null);
  }




  /**
   * 读小于1000行数据, 带样式
   * filePath 文件绝对路径
   * initSheet :
   * sheetNo: sheet页码,默认为1
   * headLineMun: 从第几行开始读取数据,默认为0, 表示从第一行开始读取
   * clazz: 返回数据List<Object> 中Object的类名
   */
  public static List<Object> readLessThan1000RowBySheet(String filePath, Sheet sheet) {
    if (!StringUtils.hasText(filePath)) {
      return null;
    }
    sheet = sheet != null &#63; sheet : initSheet;
    InputStream fileStream = null;
    try {
      fileStream = new FileInputStream(filePath);
      return EasyExcelFactory.read(fileStream, sheet);
    } catch (FileNotFoundException e) {
      log.info("找不到文件或文件路径错误, 文件:{}", filePath);
    } finally {
      try {
        if (fileStream != null) {
          fileStream.close();
        }
      } catch (IOException e) {
        log.info("excel文件读取失败, 失败原因:{}", e);
      }
    }
    return null;
  }


  /**
   * 读大于1000行数据
   *
   * @param filePath 文件觉得路径
   * @return
   */
  public static List<Object> readMoreThan1000Row(String filePath) {
    return readMoreThan1000RowBySheet(filePath, null);
  }


  /**
   * 读大于1000行数据, 带样式
   *
   * @param filePath 文件觉得路径
   * @return
   */
  public static List<Object> readMoreThan1000RowBySheet(String filePath, Sheet sheet) {
    if (!StringUtils.hasText(filePath)) {
      return null;
    }
    sheet = sheet != null &#63; sheet : initSheet;
    InputStream fileStream = null;
    try {
      fileStream = new FileInputStream(filePath);
      ExcelListener excelListener = new ExcelListener();
      EasyExcelFactory.readBySax(fileStream, sheet, excelListener);
      return excelListener.getDatas();
    } catch (FileNotFoundException e) {
      log.error("找不到文件或文件路径错误, 文件:{}", filePath);
    } finally {
      try {
        if (fileStream != null) {
          fileStream.close();
        }
      } catch (IOException e) {
        log.error("excel文件读取失败, 失败原因:{}", e);
      }
    }
    return null;
  }


  /**
   * 读大于1000行数据, 带样式
   *
   * @return
   */
  public static List<Object> readMoreThan1000RowBySheetFromInputStream(InputStream inputStream, Sheet sheet) {
    sheet = sheet != null &#63; sheet : initSheet;
    InputStream fileStream = null;
    ExcelListener excelListener = new ExcelListener();
    EasyExcelFactory.readBySax(inputStream, sheet, excelListener);
    return excelListener.getDatas();
  }


  /**
   * 生成excle
   *
   * @param filePath 绝对路径
   * @param data   数据源
   * @param head   表头
   */
  public static void writeBySimple(String filePath, List<List<Object>> data, List<String> head) {
    writeSimpleBySheet(filePath, data, head, null);
  }


  /**
   * 生成excle
   *
   * @param filePath 路径
   * @param data   数据源
   * @param sheet  excle页面样式
   * @param head   表头
   */
  public static void writeSimpleBySheet(String filePath, List<List<Object>> data, List<String> head, Sheet sheet) {
    sheet = (sheet != null) &#63; sheet : initSheet;
    if (head != null) {
      List<List<String>> list = new ArrayList<>();
      head.forEach(h -> list.add(Collections.singletonList(h)));
      sheet.setHead(list);
    }
    OutputStream outputStream = null;
    ExcelWriter writer = null;
    try {
      outputStream = new FileOutputStream(filePath);
      writer = EasyExcelFactory.getWriter(outputStream);
      writer.write1(data, sheet);
    } catch (FileNotFoundException e) {
      log.error("找不到文件或文件路径错误, 文件:{}", filePath);
    } finally {
      try {
        if (writer != null) {
          writer.finish();
        }

        if (outputStream != null) {
          outputStream.close();
        }

      } catch (IOException e) {
        log.error("excel文件导出失败, 失败原因:{}", e);
      }
    }
  }


  /**
   * 生成excle
   *
   * @param filePath 路径
   * @param data   数据源
   */
  public static void writeWithTemplate(String filePath, List<&#63; extends BaseRowModel> data) {
    writeWithTemplateAndSheet(filePath, data, null);
  }




  /**
   * 生成excle
   *
   * @param filePath 路径
   * @param data   数据源
   * @param sheet  excle页面样式
   */
  public static void writeWithTemplateAndSheet(String filePath, List<&#63; extends BaseRowModel> data, Sheet sheet) {
    if (CollectionUtils.isEmpty(data)) {
      return;
    }
    sheet = (sheet != null) &#63; sheet : initSheet;
    sheet.setClazz(data.get(0).getClass());
    OutputStream outputStream = null;
    ExcelWriter writer = null;
    try {
      outputStream = new FileOutputStream(filePath);
      writer = EasyExcelFactory.getWriter(outputStream);
      writer.write(data, sheet);
    } catch (FileNotFoundException e) {
      log.error("找不到文件或文件路径错误, 文件:{}", filePath);
    } finally {
      try {
        if (writer != null) {
          writer.finish();
        }
        if (outputStream != null) {
          outputStream.close();
        }
      } catch (IOException e) {
        log.error("excel文件导出失败, 失败原因:{}", e);
      }
    }




  }




  /**
   * 生成多Sheet的excle
   *
   * @param filePath       路径
   * @param multipleSheelPropetys
   */
  public static void writeWithMultipleSheel(String filePath, List<MultipleSheelPropety> multipleSheelPropetys) {
    if (CollectionUtils.isEmpty(multipleSheelPropetys)) {
      return;
    }
    OutputStream outputStream = null;
    ExcelWriter writer = null;
    try {
      outputStream = new FileOutputStream(filePath);
      writer = EasyExcelFactory.getWriter(outputStream);
      for (MultipleSheelPropety multipleSheelPropety : multipleSheelPropetys) {
        Sheet sheet = multipleSheelPropety.getSheet() != null &#63; multipleSheelPropety.getSheet() : initSheet;
        if (!CollectionUtils.isEmpty(multipleSheelPropety.getData())) {
          sheet.setClazz(multipleSheelPropety.getData().get(0).getClass());
        }
        writer.write(multipleSheelPropety.getData(), sheet);
      }

    } catch (FileNotFoundException e) {
      log.error("找不到文件或文件路径错误, 文件:{}", filePath);
    } finally {
      try {
        if (writer != null) {
          writer.finish();
        }

        if (outputStream != null) {
          outputStream.close();
        }
      } catch (IOException e) {
        log.error("excel文件导出失败, 失败原因:{}", e);
      }
    }
  }


  /*********************匿名内部类开始,可以提取出去******************************/
  @Data
  public static class MultipleSheelPropety {
    private List<&#63; extends BaseRowModel> data;
    private Sheet sheet;
  }

  /**
   * 解析监听器,
   * 每解析一行会回调invoke()方法。
   * 整个excel解析结束会执行doAfterAllAnalysed()方法
   *
   * @author: chenmingjian
   * @date: 19-4-3 14:11
   */
  @Getter
  @Setter
  public static class ExcelListener extends AnalysisEventListener {
    private List<Object> datas = new ArrayList<>();


    /**
     * 逐行解析
     * object : 当前行的数据
     */
    @Override
    public void invoke(Object object, AnalysisContext context) {
      //当前行
      // context.getCurrentRowNum()
      if (object != null) {
        datas.add(object);
      }
    }


    /**
     * 解析完所有数据后会调用该方法
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
      //解析结束销毁不用的资源
    }

  }


  /************************匿名内部类结束,可以提取出去***************************/
}

CommonUtil:工具类

public class CommonUtil {

/**
 * 生成32位编码,不含横线
 *
 * @return uuid串
 */
public static String getUUID() {
  String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
  return uuid.toUpperCase();
}

/**
 * 得到当前日期格式化后的字符串,格式:yyyy-MM-dd(年-月-日)
 * @return 当前日期格式化后的字符串
 */
public static String getTodayStr(){
  return new SimpleDateFormat("yyyy-MM-dd").format(new Date()) ;
}

/**
 * 将对象转化成json
 *
 * @param t
 * @return
 * @throws JsonProcessingException
 */
public static <T> String toJson(T t) throws JsonProcessingException {
  return OBJECT_MAPPER.get().writeValueAsString(t);
}

}

UserPojoRes:实体类

@Setter
@Getter
@ToString
public class UserPojoRes extends BaseRowModel implements Serializable {
  private static final long serialVersionUID = -2145503717390503506L;

  /**
   * 主键
   */
  @ExcelProperty(value = "ID", index = 0)
  private String id;
  /**
   * 姓名
   */
  @ExcelProperty(value = "用户名", index = 1)
  private String name;

  public UserPojoRes(String id, String name) {
    this.id = id;
    this.name = name;
  }

  public UserPojoRes(){


  }
}

验证

模板下载

这里将模板文件放在resources

@GetMapping("/exportExcelTempalte")
@ApiOperation(value = "下载导入模板")
public void exportExcelTempalte(HttpServletResponse response) throws Exception {
  //Resource目录中的文件
  String filePath = "/excels/导入模板.xlsx";
  ClassPathResource classPathResource = new ClassPathResource(filePath);
  Workbook workbook=WorkbookFactory.create(classPathResource.getInputStream());
  ExcelUtil.downLoadExcel("导入模板.xlsx", response, workbook);
}

Excel文件导入

@PostMapping("/importExcel")
@ApiOperation(value = "Excel文件导入")
public Response importExcel(HttpServletRequest request, MultipartFile file, HttpServletResponse response) throws Exception {
  List<Object> objects = ExcelUtil.readMoreThan1000RowBySheetFromInputStream(file.getInputStream(),null);
  List<UserPojoRes> list = new ArrayList<>();
  for (Object o : objects) {
    UserPojoRes userPojoRes = new UserPojoRes();
    List<String> stringList = (List<String>) o;
    userPojoRes.setId(stringList.get(0) != null &#63; stringList.get(0).toString() : "");
    userPojoRes.setName(stringList.get(1) != null &#63; stringList.get(0).toString() : "");
    list.add(userPojoRes);
  }
  String json = CommonUtil.toJson(list);
  return new Response(json);
}

SpringBoot中EasyExcel如何实现Excel文件的导入导出

Excel文件导出

@Value("${project.tmp.files.path}")
public String filesPath;

@GetMapping("/exportExcel")
@ApiOperation(value = "Excel文件导出")
public void exportExcel(HttpServletResponse response) throws Exception {

  //创建临时文件
  String path = filesPath + CommonUtil.getUUID() + ".xlsx";
  List<UserPojoRes> list = new ArrayList<>();
  UserPojoRes userPojoRes = new UserPojoRes("009", "张三");
  UserPojoRes userPojoRes1 = new UserPojoRes("009", "李四");
  list.add(userPojoRes);
  list.add(userPojoRes1);
  ExcelUtil.writeWithTemplate(path, list);
  // 根据excel创建对象
  Workbook workbook = WorkbookFactory.create(new FileInputStream(path));
  String fileName = "用户模块" + CommonUtil.getTodayStr() + ".xlsx";
  ExcelUtil.downLoadExcel(fileName, response, workbook);
}

SpringBoot中EasyExcel如何实现Excel文件的导入导出

关于SpringBoot中EasyExcel如何实现Excel文件的导入导出就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 怎么在SpringBoot中利用EasyExcel导入Excel文件
  2. java如何使用EasyExcel导入导出excel

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot excel easyexcel

上一篇:JavaScript 模块执行器如何实现

下一篇:JS如何跨浏览器进行解析XML应用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》