springboot中使用easypoi导出excel表格
作者:互联网
- 引入pom依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.2.0</version>
</dependency>
两种方式(第一种是不通过模版进行导出,第二种是通过模版进行导出)
- 无模版导出(使用注解)
无模版导出依赖对象
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable{
/**
* 姓名
**/
@Excel(name = "姓名", width = 30 ,isImportField = "true_st")
private String userName;
/**
* 手机号
**/
@Excel(name = "手机号", width = 30 ,isImportField = "true_st")
private String phone;
}
public void export(){
//文件里面内容
List<User> pageList = certPersonMapper.selectList();
Workbook workbook = ExcelExportUtil.exportExcel(
new ExportParams("xx系统","人员信息"),User.class, pageList);
try {
//文件生成路径
FileOutputStream fos = new FileOutputStream("E:\\person_download.xls");
workbook.write(fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
- 使用模版导出
public void exportCustomExcel(){
//1.获取废品的excel模板
TemplateExportParams params = new TemplateExportParams("E:\\人员信息.xlsx");
//2.获取所有过磅数据
List<User> allMap = certPersonMapper.selectList();
Map<String, Object> map = new HashMap<String, Object>(100);
map.put("poundLogList", allMap);
//3.执行excel导出
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
//4.创建文件存储路径
File saveFile = new File("E:\\");
if (!saveFile.exists()) {
saveFile.mkdirs();
}
FileOutputStream fos;
String filePath;
try {
//4.写入文件
filePath = saveFile + "\\person_download1.xls";
fos = new FileOutputStream(filePath);
workbook.write(fos);
fos.close();
} catch (Exception e) {
log.error("IOException={}", e.getMessage(), e);
}
}
- 模本内容
标签:springboot,fos,excel,导出,new,easypoi,afterturn,cn 来源: https://blog.csdn.net/Song_ly/article/details/117119991