zoukankan      html  css  js  c++  java
  • SpringBoot使用EasyPoi进行数据导入导出Excel(一)

    在实际项目开发中,对于Excel的导入导出还是很常见的需求,比如说将数据根据模板批量导入到数据库中,以及将数据库中的数据批量导出陈Excel的形式
    现有需求:

    1. 下载固定的导入Excel模板
    2. 导入Excel中的数据进数据库
    3. 将数据进行Ecel导出
      本篇文章,先总结excel静态模板文件的下载

    一. 准备工作

    1. 准备静态文件

    2. 导入 EasyPOI 的依赖

      <properties>
          <axis2.version>1.7.9</axis2.version>
          <easypoi.version>4.1.0</easypoi.version>
      </properties>
      <dependencies>
          <dependency>
              <groupId>cn.afterturn</groupId>
              <artifactId>easypoi-spring-boot-starter</artifactId>
              <version>${easypoi.version}</version>
          </dependency>
          <dependency>
              <groupId>cn.afterturn</groupId>
              <artifactId>easypoi-base</artifactId>
              <version>${easypoi.version}</version>
          </dependency>
          <dependency>
              <groupId>cn.afterturn</groupId>
              <artifactId>easypoi-web</artifactId>
              <version>${easypoi.version}</version>
          </dependency>
          <dependency>
              <groupId>cn.afterturn</groupId>
              <artifactId>easypoi-annotation</artifactId>
              <version>${easypoi.version}</version>
          </dependency>
      </dependencies>
      

    二. 使用easypoi进行静态模板的导出

    excel静态模板下载,有两种方式:

    第一种,在http头中指定输出文件流的类型为"application/vnd.ms-excel"类型时,输出流时就不需要添加输出文件的后缀名;

        @GetMapping("/templateDownload")
        public ResponseEntity<String> templateDownload(@PathVariable("organizationId")Long tenantId,
                                                        HttpServletResponse response ) {
            try {
                // 获取资源中的模板文件
                ClassPathResource resource = new ClassPathResource("static\拉线-设备主数据导入模板.xlsx");
                InputStream inputStream = resource.getInputStream();
                Workbook wb = WorkbookFactory.create(inputStream);
                String fileName="拉线-设备主数据导入模板";
                response.setCharacterEncoding("UTF-8");
                response.setHeader("content-Type", "application/vnd.ms-excel");
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                wb.write(response.getOutputStream());
                return Results.success();
            }catch (IOException e){
                return Results.error(e.getMessage());
            }
    

    第二种,指定文件流的类型为"multipart/form-data"时,输出流时需要判断文件是.xls/.xlsx,并且加上后缀名。

        @GetMapping("/templateDownload1")
        public ResponseEntity<String> templateDownload1(@PathVariable("organizationId")Long tenantId,
                                                       HttpServletResponse response ) {
            try {
                // 获取资源中的模板文件
                ClassPathResource resource = new ClassPathResource("static\拉线-设备主数据导入模板.xlsx");
                InputStream inputStream = resource.getInputStream();
                // 根据不同excel创建不同对象,Excel2003版本-->HSSFWorkbook,Excel2007版本-->XSSFWorkbook
                Workbook wb = WorkbookFactory.create(inputStream);
                response.reset();
                response.setContentType("multipart/form-data");
                String fileName="拉线-设备主数据导入模板";
                // 判断excel文件类型,下载获取到的模板并重新命名
                System.out.println(wb.getClass().getSimpleName());
                if (wb.getClass().getSimpleName().equals("HSSFWorkbook")) {
                    response.setHeader("Content-Disposition",
                            "attachment; filename=" + "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
                } else {
                    response.setHeader("Content-Disposition",
                            "attachment; filename=" + "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
                }
                wb.write(response.getOutputStream());
                return Results.success();
            }catch (IOException e){
                return Results.error(e.getMessage());
            }
    

    这部分我只是大概写了一下测试实现,在实际的工作中,导入导出等代码肯定是有特别高的复用率的,可以将代码中其中一部分抽离出来一个公用的工具类进行调用

    实战为王,记录在工作中技术使用的点点滴滴
  • 相关阅读:
    background及background-size
    -ms-,-moz-,-webkit-,-o-含义
    &:first-of-type含义
    ES6的Promise对象
    LightOJ 1029 Civil and Evil Engineer最小生成树和最大生成树
    快速幂模板
    poj2965 The Pilots Brothers' refrigerator 枚举或DFS
    poj1753Flip Game(枚举+DFS)
    POJ 1751Highways
    HDU 1875 畅通工程再续 prim模板题
  • 原文地址:https://www.cnblogs.com/kaikai-wanna-know/p/12581047.html
Copyright © 2011-2022 走看看