当前位置:网站首页>File excel export
File excel export
2022-06-11 11:17:00 【Grade III code farmer 666】
1. rely on
<!--easyExcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.4</version>
</dependency>
2.controller
@RequestMapping(method = RequestMethod.GET, value = "/export")
@Accessable(moduleName = " export ", name = " Inquire about ", function = "EnrollInfo", requireRole = false, requireLogin = false, log = false)
@ApiOperation(value = " export ")
public void export(HttpServletResponse response, String productId) {
this.codeService.export(response, productId);
}
3.serviceImpl Function realization
@Override
public void export(HttpServletResponse response, String productId) {
String fileName = filePath;
log.info(" Export file path :{}", fileName);
ExcelWriter excelWriter = null;
WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
fileName += "/export/" + " Code table " + System.currentTimeMillis() + ".xlsx";
excelWriter = EasyExcel.write(fileName, CodeExcelVo.class).build();
excelWriter.write(data_export(productId), writeSheet);
// Don't forget finish Will help shut down the flow
excelWriter.finish();
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.DEFAULT.encode(" Code table ", StandardCharsets.UTF_8));
response.setContentType("application/octet-stream");
try {
byte[] bytes = Files.toByteArray(new File(fileName));
ServletOutputStream stream = response.getOutputStream();
stream.write(bytes);
stream.flush();
stream.close();
} catch (IOException e) {
throw new BusinessException(" Please try again ");
}
}
// The query needs to export the set
private List<CodeExcelVo> data_export(String productId) {
List<CodeExcelVo> list = new ArrayList<>();
LambdaQueryWrapper<Code> wrapper = new LambdaQueryWrapper<>();
List<Code> codes = codeMapper.selectList(wrapper.eq(Code::getDataStatus, 0).eq(Code::getProductId, productId));
codes.forEach(c -> {
CodeExcelVo vo = new CodeExcelVo();
BeanUtils.copyProperties(c, vo);
list.add(vo);
});
// Mapping values
List<User> allUser = userDao.getAllUser();
Map<Long, String> userMap = allUser.stream().collect(Collectors.toMap(User::getId, User::getUserName));
if (!ObjectUtils.isEmpty(list)) {
list.forEach(a -> {
// Whether to spend
a.setIsConsumeStr(a.getIsConsume() == 1 ? " Have consumed " : " Not consumed ");
if (!ObjectUtils.isEmpty(a.getConsumeBy())) {
a.setConsumeBy(userMap.get(Long.valueOf(a.getConsumeBy())));
}
});
}
return list;
}
4.Code It maps to —>CodeExcelVo export
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
@Data
@ApiModel(value = " Code table excel Mapping entities ")
@Accessors(chain = true)
public class CodeExcelVo {
@TableId(type = IdType.AUTO)
private String id;
@ExcelProperty(" The product name ")
@ApiModelProperty(required = false, value = " The product name ")
private String productName;
@ExcelIgnore
@ApiModelProperty(required = false, value = " product id")
private String productId;
@ExcelProperty(" Product model ")
@ApiModelProperty(required = false, value = " Product model ")
private String productModel;
@ExcelProperty(" Code year ")
@ApiModelProperty(required = false, value = " Code year ")
private String codeYear;
@ExcelProperty(" Coding batch ")
@ApiModelProperty(required = false, value = " Coding batch ")
private int codeBatch;
@ExcelProperty("S/N code ")
@ApiModelProperty(required = false, value = "S/N code ")
private String snCode;
@ExcelProperty("mac code ")
@ApiModelProperty(required = false, value = "mac code ")
private String macCode;
@ExcelProperty(" Whether to spend ")
@ApiModelProperty(required = false, value = " Whether to spend ")
private String isConsumeStr;
@ExcelIgnore
@ApiModelProperty(required = false, value = " Whether to spend ")
private int isConsume;
@ExcelProperty(" dissipate ")
@ApiModelProperty(required = false, value = " dissipate ")
private Date consumeTime;
@ExcelProperty(" Consumer ")
@ApiModelProperty(required = false, value = " Consumer ")
private String consumeBy;
}
边栏推荐
- 袋鼠云数栈基于CBO在Spark SQL优化上的探索
- Problems encountered when using nailing intranet to penetrate and upload PHP projects
- Jerry's ble spp open pin_ Code function [chapter]
- 使用Yolov5训练自己制作的数据集,快速上手
- nft数字藏品app系统搭建
- 企业微信小程序避坑指南,欢迎补充。。。
- 小白在同花顺上直接开户是安全的吗?
- 2022健博会,北京大健康产业展,艾灸健康展,北京健康服务展
- 迭代器模式--沙场秋点兵
- Digital collection app applet official account source code
猜你喜欢
![Jerry's acquisition of ble OTA dual backup upgrade (can only be used for chips above 4mbits) [article]](/img/25/e90d23f5c3d2aa6790538b289137bf.png)
Jerry's acquisition of ble OTA dual backup upgrade (can only be used for chips above 4mbits) [article]

Only when you find your own advantages can you work tirelessly and get twice the result with half the effort!

把程序写进微控制器里可以更方便快捷的控制电机正反转

封装组件系列-(一)-插槽及动态组件

How to form a good habit? By perseverance? By determination? None of them!

Droid-slam: depth vision slam for monocular and binocular rgbd cameras

设置默认收货地址【项目 商城】

MySQL optimized learning diary 10 - locking mechanism

Electron桌面端开发(开发一个闹钟【完结】)

DROID-SLAM: 用于单目双目RGBD相机的深度视觉SLAM
随机推荐
杰理之获取 BLE OTA 双备份升级(只能用于 4Mbits 以上的芯片)【篇】
Installing redis in CentOS 7 environment
【C语言】anonymous/unnamed struct&&union
2022健博会,北京大健康产业展,艾灸健康展,北京健康服务展
Droid-slam: depth vision slam for monocular and binocular rgbd cameras
Package component series - (I) - slots and dynamic components
Tu ne peux pas être libre sans richesse?
使用Yolov5训练好模型调用电脑自带摄像头时出现问题:TypeError: argument of type “int‘ is not iterable的解决方法
AcWing 1353. Ski resort design (greedy)
Report on various activity plans of safety month 2022 (28 pages)
SpingBoot+Quartrz生产环境的应用支持分布式、自定义corn、反射执行多任务
VOC格式数据集转yolo格式数据集的方法
SQL query statement optimization
Can't you be free without wealth?
Introduction to database system -- Chapter 2 -- relational database (2.4 relational algebra)
Vscode——vscode 多窗口名字配置成项目名字
使用国产MCU(国民技术 N32G031F8S7) 实现 PWM+DMA 控制 WS2812
Xiao P weekly Vol.08
nft数字藏品app系统搭建
数字藏品app小程序公众号系统开发