springboot, easypoi导入导出示例及配置
作者:互联网
配置导入easypoi 包
<!-- easypoi -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.8.2</version>
</dependency>
<!-- easypoi -->
实体类文件配置
@Excel(name = "产品名称",orderNum = "1")
package com.taixin.rail.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class HarmlessVo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "产品名称")
@Excel(name = "产品名称",orderNum = "1")
private String product;
@ApiModelProperty(value = "屠宰场名称")
@Excel(name = "屠宰场名称",orderNum = "2")
private String slaughterhouse;
@ApiModelProperty(value = "耳标")
@Excel(name = "耳标",orderNum = "3")
private String earTag;
@ApiModelProperty(value = "数量")
@Excel(name = "数量",orderNum = "4")
private Integer harmlessNo;
@ApiModelProperty(value = "操作时间")
@Excel(name = "操作时间",orderNum = "5")
private String createTime;
}
导出使用
@PostMapping("/exportExcel")
public void exportExcel(HttpServletResponse response, @RequestBody HarmlessQo qo) {
//获取要导出的数据
List<HarmlessVo> list = harmlessService.exportExcel(qo);
ExcelUtil.exportExcel(list, qo.getXlsTitle(), qo.getXlsTitle(), HarmlessVo.class, qo.getXlsTitle() + ".xls", response);
}
导入使用
/**
* 导入
*
* @param createBy
* @param file
* @return
*/
@PostMapping(value = "/importExcel")
@ResponseBody
public Result importExcel(@RequestParam("file") MultipartFile file, @RequestParam("createBy") String createBy) {
if (file.isEmpty()) {
return Result.error(404, "文件不能为空");
}
return Result.ok(harmlessService.importExcel(file, createBy));
}
@Override
public Integer importExcel(MultipartFile file, String createBy) {
List<HarmlessVo> list = ExcelUtil.importExcel(file, 0, 1, HarmlessVo.class);
//添加数据
Integer num = harmlessMapper.importExcel(list);
return num;
}
工具类下载 ExcelUtil.java
标签:springboot,示例,Excel,value,ApiModelProperty,importExcel,private,file,easypoi 来源: https://blog.csdn.net/jia722984/article/details/120346178