SpringBoot+easypoi实现数据的Excel导出
作者:进击的_菜鸡 时间:2023-04-05 12:27:19
本文实例为大家分享了SpringBoot+easypoi实现数据的Excel导出的具体代码,供大家参考,具体内容如下
maven
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.1.0</version>
</dependency>
Controller层
// 接口不需要返回值
@RequestMapping(value = "/export-activity-data")
public void exportActivityData(@RequestParam String activityType,
@RequestParam String activityState,
@RequestParam String queryValue,
@RequestParam String levelValue,
@RequestParam String startTime,
@RequestParam String endTime, HttpServletResponse response) {
try {
manageService.exportActivityData(TFActivityQueryParam.builder()
.activityState(activityState)
.activityType(activityType)
.queryValue(queryValue)
.levelValue(levelValue)
.startTime("".equals(endTime) ? null : new Date(DateTime.parse(startTime).getMillis()))
.endTime("".equals(endTime) ? null : new Date(DateTime.parse(endTime).getMillis())).build(), response);
} catch (IOException e) {
log.info( "导出失败", e);
}
}
service层
public void exportActivityData(TFActivityQueryParam param, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode("活动综合数据.xls", "UTF-8"));
val out = response.getOutputStream();
List<TFActivityQueryResult> tfActivityList = getTFActivityList(param);
List<TFActivityQueryResultExportDto> exportDtoList = new ArrayList<>();
tfActivityList.forEach(activity -> {
TFActivityQueryResultExportDto convert = TFActivityQueryResultExportDto.convert(activity);
if (activity.getLevelType().equals("0")) {
convert.setAffiliation("云南省");
} else {
EparchyCode eparchyCode = getEparchyCodeList().stream()
.filter(code -> code.getEparchyCode().equals(activity.getEparchyCode()))
.collect(Collectors.toList()).get(0);
convert.setAffiliation(eparchyCode.getEparchyShortName());
}
exportDtoList.add(convert);
});
Workbook workbook = ExcelExportUtil.exportExcel(
new ExportParams("活动综合数据", "活动"), TFActivityQueryResultExportDto.class, exportDtoList);
log.info("workbook: {}", workbook);
workbook.write(out);
out.close();
}
数据bean
public class TFActivityQueryResultExportDto {
@Excel(name = "活动编码", width = 20)
private String activityCode;
@Excel(name = "活动名称", width = 20)
private String activityName;
@Excel(name = "活动标题", width = 20)
private String activityTitle;
@Excel(name = "归属", width = 20)
private String affiliation;
@Excel(name = "活动类型", width = 20)
private String activityType;
@Excel(name = "活动时间", width = 30)
private String activityTime;
@Excel(name = "活动状态", width = 20)
private String activityState;
@Excel(name = "备注", width = 30)
private String remark;
@Excel(name = "创建时间", width = 30)
private String timeCreate;
@Excel(name = "最新操作人", width = 30)
private String operatorName;
@Excel(name = "更新时间", width = 30)
private String timeUpdate;
}
来源:https://blog.csdn.net/weixin_40325475/article/details/102567163
标签:SpringBoot,easypoi,Excel
0
投稿
猜你喜欢
Android应用程序转到后台并回到前台判断方法
2022-11-12 19:49:35
Android源码系列之深入理解ImageView的ScaleType属性
2022-12-14 07:51:59
Android实现单选按钮
2021-11-20 18:32:53
SpringCloud如何解决服务之间的通信问题
2023-03-27 03:13:05
浅谈使用Java Web获取客户端真实IP的方法示例详解
2022-04-01 19:47:47
C#ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决方法
2021-06-04 20:11:14
50 道Java 线程面试题(经典)
2023-11-23 19:47:37
C# Winform 调用系统接口操作 INI 配置文件的代码
2023-03-04 11:49:54
java递归设置层级菜单的实现
2023-03-05 14:14:57
C#反射在实际应用中的实例代码
2022-11-25 05:06:21
java-spark中各种常用算子的写法示例
2023-04-28 23:21:01
C#基础知识之this关键字介绍
2022-02-08 10:59:16
C#使用windows服务开启应用程序的方法
2022-08-14 04:37:14
Springboot配置文件内容加密代码实例
2022-09-13 05:56:09
解决java.util.NoSuchElementException异常的问题
2023-02-10 08:26:46
Maven配置多仓库无效的解决
2023-11-29 04:37:10
老生常谈C/C++内存管理
2022-05-07 02:17:10
Java多线程的用法详解
2021-10-29 19:20:59
一文了解Java读写锁ReentrantReadWriteLock的使用
2023-10-12 19:28:21
C#中XmlTextWriter读写xml文件详细介绍
2022-01-26 05:13:48