SpringBoot使用easypoi根据设置模板导出数据和较大数据量导出

使用这个前需要引入依赖

```java
	<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17-beta1</version>
        </dependency>

1.十万以内数据导出

public void getUpload(HttpServletResponse response,
                                   @RequestParam(defaultValue = "1") int pageNumber,
                                   @RequestParam(defaultValue = "20") int pageSize,
                                  )throws IOException {
       
        Map param = Maps.newHashMap();
      //获取要导出的数据
        List<User> exports = UserMapper.getUserAll(param);
        Map map = Maps.newHashMap();
        map.put("list",exports);
       //获取导出模板地址
        ClassPathResource classPathResource = new ClassPathResource("static/importTemps/cbsj.xlsx");
        String path = classPathResource.getPath();
        /** 一开始下的这个 获取绝对路径会报错{cannot be resolved to absolute file path because it does not reside in the file system},所以改为上面的即可解决问题
           String path = classPathResource.getFile().getPath();
        **/
        
        TemplateExportParams templateExportParams1 = new TemplateExportParams(path);
        Workbook sheets = ExcelExportUtil.exportExcel(templateExportParams1,map);

        String fileName = "导出数据.xlsx";
        //取得输出流
        OutputStream out = response.getOutputStream();
        //清空输出流
        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
        //写入文件
        sheets.write(out);
        sheets.close();
        //关闭流
        out.close();
    }

模板样式为:在这里插入图片描述

文件在项目中地址为:在这里插入图片描述

2.大数据导出

    public void bigExport(HttpServletResponse response) throws Exception {
      
       List<User> list= UserMapper.getUserAll(param);
        Workbook workbook = null;
        ExportParams params = new ExportParams("大数据导出", "大数据导出");
        if(list.size()>100000){
            workbook = ExcelExportUtil.exportBigExcel(params, ResourcePoolExport.class, list);
        }else{
            workbook = ExcelExportUtil.exportExcel(params,
                    ResourcePoolExport.class, list);
        }

        //while (list.size()>0){
        //    List<ResourcePoolExport> list1 = list.subList(0,list.size() > 10000 ? 10000 : list.size());
        //    workbook = ExcelExportUtil.exportBigExcel(params, ResourcePoolExport.class, list1);
        //    list1.clear();
        //}
        ExcelExportUtil.closeExportBigExcel();
        String fileName = "大数据导出.xlsx";
        //取得输出流v
        OutputStream out = response.getOutputStream();
        //清空输出流
        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");


        response.setHeader("Content-Disposition",
                "attachment;fileName=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
        //写入文件
        workbook.write(out);
        workbook.close();
        //关闭流
        out.close();

    }
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在你的Spring Boot项目中添加Easypoi的依赖。可以在pom.xml中添加以下代码: ```xml <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.2.0</version> </dependency> ``` 接下来,你需要定义一个实体类作为导出数据模型,并使用注解来定义每个字段的名称和格式。例如: ```java public class User { @Excel(name = "编号", orderNum = "0") private Integer id; @Excel(name = "用户名", orderNum = "1") private String username; @Excel(name = "注册时间", orderNum = "2", format = "yyyy-MM-dd HH:mm:ss") private Date createTime; // 省略getter和setter方法 } ``` 在上面的代码中,我们使用了@Excel注解来定义导出字段的名称、顺序和格式。其中,name属性指定了字段的名称,orderNum属性指定了字段在Excel文件中的顺序,format属性指定了时间字段的格式。 接下来,你可以在控制器中定义一个导出Excel文件的方法。例如: ```java @GetMapping("/export") public void export(HttpServletResponse response) throws IOException { List<User> userList = userService.getUserList(); // 定义导出的Excel文件名称 String fileName = "用户列表.xlsx"; // 定义导出数据表格 ExportParams exportParams = new ExportParams("用户列表", "用户信息"); // 创建Excel文件并写入数据 Workbook workbook = ExcelExportUtil.exportExcel(exportParams, User.class, userList); // 设置响应头 response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); // 将Excel文件写入响应输出流中 workbook.write(response.getOutputStream()); } ``` 在上面的代码中,我们使用Easypoi提供的ExcelExportUtil工具类来创建导出的Excel文件。其中,ExportParams对象用于定义导出文件的标题和表头,User.class用于定义数据模型,userList是要导出数据集合。 最后,我们将Excel文件写入响应输出流中,以实现文件下载。 注意:如果你的时间字段是字符串类型,可以在导出时先将其转换为Date类型,然后再使用@Excel注解定义格式。例如: ```java public class User { @Excel(name = "编号", orderNum = "0") private Integer id; @Excel(name = "用户名", orderNum = "1") private String username; @Excel(name = "注册时间", orderNum = "2", format = "yyyy-MM-dd HH:mm:ss") private Date createTime; // 定义createTime的getter方法 public Date getCreateTime() { try { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(this.createTime); } catch (ParseException e) { e.printStackTrace(); return null; } } // 定义createTime的setter方法 public void setCreateTime(String createTime) { this.createTime = createTime; } } ``` 在上面的代码中,我们将createTime字段从字符串类型转换为Date类型,并在getter方法中返回Date类型的值。这样,就可以使用@Excel注解定义时间格式了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值