爱玩科技网
您的当前位置:首页SpringBoot+easypoi实现数据的Excel导出

SpringBoot+easypoi实现数据的Excel导出

来源:爱玩科技网
SpringBoot+easypoi实现数据的Excel导出

本⽂实例为⼤家分享了SpringBoot+easypoi实现数据的Excel导出的具体代码,供⼤家参考,具体内容如下maven

cn.afterturn

easypoi-spring-boot-starter 4.1.0

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( \"导出失败\ } }

service层

public void exportActivityData(TFActivityQueryParam param, HttpServletResponse response) throws IOException { response.setCharacterEncoding(\"UTF-8\");

response.setHeader(\"content-Type\ response.setHeader(\"Content-Disposition\

\"attachment;filename=\" + URLEncoder.encode(\"活动综合数据.xls\ val out = response.getOutputStream();

List tfActivityList = getTFActivityList(param); List 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.write(out); out.close(); }

数据bean

public class TFActivityQueryResultExportDto { @Excel(name = \"活动编码\ private String activityCode;

@Excel(name = \"活动名称\ private String activityName;

@Excel(name = \"活动标题\ private String activityTitle;

@Excel(name = \"归属\ private String affiliation;

@Excel(name = \"活动类型\ private String activityType;

@Excel(name = \"活动时间\ private String activityTime;

@Excel(name = \"活动状态\ private String activityState;

@Excel(name = \"备注\ private String remark;

@Excel(name = \"创建时间\ private String timeCreate;

@Excel(name = \"最新操作⼈\ private String operatorName;

@Excel(name = \"更新时间\ private String timeUpdate;}

以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

因篇幅问题不能全部显示,请点此查看更多更全内容