SpringBoot集成EasyPoi的基本使用(注解导出和模板导出)

首先,在SpringBoot中使用EasyPoi少不了包的集成,在这里提供了EasyPoi的包:

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>4.0.0</version>
</dependency>

对于浏览器中的表的数据,需要在我们的实体类中将数据的字段与属性一一对应进行封装,使用@Excel注解进行注解导出,每列数据的列名使用@Excel(name属性),代码如下所示:

public class StudentClient implements Serializable {

    @Excel(name = "手机号")
    private String mobile;

    @Excel(name = "姓名")
    private String name;

对于一些字段,查找出的数据与导出的数据内容不一致,属于一种对应关系,如:1对应男,2对应女,想要进行对应关系的转换,可使用@Excelreplace属性;如下所示:

@Excel(name = "学生性别", replace = { "男_1", "女_2" })
private int sex;

具体其它一些属性暂不细说,有需要的小伙伴可查看开发文档:

2.4 注解变种-代码注解导出 - Powered by MinDoc

一:Excel文件本地导出

在数据所对应的实体类封装好之后,就可以进行导出功能的代码编写了。做一个简单的本地导出数据;

List<StudentClient > list = new ArrayList<StudentClient>();
for (int i = 0; i < 5; i++) {
    StudentClient client = new StudentClient();
    client.setClientName("小廖" + i);
    client.setClientPhone("438" + i);
    client.setRemark("测试" + i);
    list.add(client);
}
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("测试", "测试"), StudentClient.class, list);
File savefile = new File("D:/excel/");
if (!savefile.exists()) {
    savefile.mkdirs();
}
FileOutputStream fos = new FileOutputStream("D:/excel/ExcelExport.xlsx");
workbook.write(fos);
fos.close();

感谢爱心观众小廖的参加,导出数据效果图如下:

二:浏览器导出Excel文件

在平常的开发中,多用于浏览器数据导出,上面算一个小小的入门;在浏览器中数据导出文件;获取响应对象进行编码格式等一系列设置;然后使用流进行数据输出就OK了。简简单单的操作,快速上手。

Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, sheetName), Class, Object);
HttpServletResponse response = WebUtils.response();// 获取响应对象
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");// 设置编码格式
String fileName = URLEncoder.encode(title, "utf-8");// 设置文件名和格式
response.setHeader("Content-dispodition", "attachment;filename="+fileName);
ServletOutputStream outputStream = response.getOutputStream();// 获取输出流
workbook.write(outputStream);// 输出
outputStream.close();// 关流


二:模板导出

使用方法大同小异,还是需要封装一个导出的对象实体类

 在业务的处理当中比较于注解导出有所不同的地方是:

outBoundOrderClient.setOutBoundOrderFreightListClients(outBoundList);
        Map<String, Object> map = new HashMap<>();
        map.put("outBoundOrderClient", outBoundOrderClient);
        //1.获取excel模板
        ClassPathResource classPathResource = new ClassPathResource("template/outboundDeliveryOrderTemplate.xlsx");
        TemplateExportParams params = new TemplateExportParams(classPathResource.getPath());
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        HttpServletResponse response = WebUtils.response();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;

这里就是比较重要的一段,最后的封装是使用map进行封装,后面的模板中会使用到表达式,跟此处的key-value有关系,模板别忘了放置到resources中

最后说一说他的表达式:

表达式的开头:

  

直接贴图,使用比较简单,API中也有很清楚的说明;
文章到这里就结束了!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值