wei_shuo的个人主页
wei_shuo的学习社区
Hello World !
需求分析
- 页面表格的数据下载,保存到Excel表格
- 搜索后的数据点击下载,下载的数据需要是搜索后的数据
- Controller
HTTP 响应对象:
httpServletResponse
BalanceDownload.class:
数据对象的类型
list:
导出的数据列表
merchant_balance:
merchant_balance@GetMapping("download") @ApiOperation("下载数据") public void balanceDownload(ReqMerchantBalanceQuery query) { List<BalanceDownload> list = merchantBalanceService.balanceDownload(query); try { ExcelTools.download(httpServletResponse, BalanceDownload.class, list, "merchant_balance"); } catch (IOException e) { throw new RuntimeException(e); } }
- ReqMerchantBalanceQuery.java(请求体:前端搜索后传递过来的请求参数)
@Data public class ReqMerchantBalanceQuery extends PageQuery { @ApiModelProperty("商户号") @IsMSearch(field = SettleTransactionDetailCol.MERCHANT_NO) private String merchantNo; @ApiModelProperty("币种") @IsMSearch(field = SettleTransactionDetailCol.SETTLE_CURRENCY) private String currency; }
Excel工具类
public class ExcelTools { public ExcelTools() { } public static <T> List<T> excelToList(Class<T> clazz, InputStream is) { List<T> list = new ArrayList(); EasyExcel.read(is, clazz, new PageReadListener((l) -> { list.addAll(l); })).sheet().doRead(); return list; } public static <T> void download(HttpServletResponse response, Class<T> clazz, List<T> data, String type) throws IOException { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + createFileName(type) + ".xlsx"); HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle()); ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy(); ((ExcelWriterSheetBuilder)((ExcelWriterSheetBuilder)EasyExcel.write(response.getOutputStream(), clazz).sheet("模板").registerWriteHandler(horizontalCellStyleStrategy)).registerWriteHandler(widthStyleStrategy)).doWrite(data); } public static <T> void download(HttpServletResponse response, Class<T> clazz, List<T> data, String type, LinkedHashSet<String> includeColumnFiledNames) throws IOException { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + createFileName(type) + ".xlsx"); HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle()); ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy(); ((ExcelWriterSheetBuilder)((ExcelWriterSheetBuilder)((ExcelWriterBuilder)EasyExcel.write(response.getOutputStream(), clazz).includeColumnFieldNames(includeColumnFiledNames)).sheet("模板").registerWriteHandler(horizontalCellStyleStrategy)).registerWriteHandler(widthStyleStrategy)).doWrite(data); } public static String createFileName(String type) { return type + "-" + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); } }
- Service
query.setCurrency(query.getCurrency().toUpperCase()):
将传递的币种进行大写转换
merchantSettleBalanceRepo.balanceListDownload(query):
方法获取了一个List
类型的数据列表list
list.stream().map(……).collect(Collectors.toList()):
使用流式操作对list
进行转换,将每个MerchantSettleBalance
对象映射为一个BalanceDownload
对象;使用Collectors.toList()
方法将转换后的结果收集为一个List
对象,并将其返回public List<BalanceDownload> balanceDownload(ReqMerchantBalanceQuery query) { if (query.getCurrency() != null) { query.setCurrency(query.getCurrency().toUpperCase()); } List<MerchantSettleBalance> list = merchantSettleBalanceRepo.balanceListDownload(query); List<BalanceDownload> collect = list.stream().map(bean -> new BalanceDownload( bean.getMerchantNo(), bean.getSettleCurrency(), Amount.getInstance(bean.getSettleCurrency(), bean.getTotalSettleAmount()).getShowAmount(), Amount.getInstance(bean.getSettleCurrency(), bean.getUnWithdrawAmount()).getShowAmount(), Amount.getInstance(bean.getSettleCurrency(), bean.getWithdrawedAmount()).getShowAmount(), Amount.getInstance(bean.getSettleCurrency(), bean.getAuditWithdrawAmount()).getShowAmount(), Amount.getInstance(bean.getSettleCurrency(), bean.getToBeSettledAmount()).getShowAmount(), Amount.getInstance(bean.getSettleCurrency(), bean.getCashDepositAmount()).getShowAmount() )).collect(Collectors.toList()); return collect; }
- Repo
List<MerchantSettleBalance> balanceListDownload(ReqMerchantBalanceQuery query);
- Impl
baseMapper.selectList(MSearchTools.createQueryWrapper(MerchantSettleBalance.class, query)):
使用给定的查询条件query
查询数据库中的MerchantSettleBalance
实体,并返回查询结果列表@Override public List<MerchantSettleBalance> balanceListDownload(ReqMerchantBalanceQuery query) { return baseMapper.selectList(MSearchTools.createQueryWrapper(MerchantSettleBalance.class, query)); }
- BalanceDownload.java(下载Excel中展示字段)
@Data @AllArgsConstructor @NoArgsConstructor @Builder public class BalanceDownload { @ExcelProperty("Merchant No") private String merchantNo; @ApiModelProperty("Currency") private String currency; @ApiModelProperty("TotalSettle Amount") private String totalSettleAmount; @ApiModelProperty("CanWithdraw Amount") private String canWithdrawAmount; @ApiModelProperty("Withdrawed Amount") private String withdrawedAmount; @ApiModelProperty("AuditWithDraw Amount") private String auditWithdrawAmount; @ApiModelProperty("ToBeSettle Amount") private String toBeSettleAmount; @ApiModelProperty("CashDeposit Amount") private String cashDepositAmount; }
测试
- Postman接口调试
结语:创作不易,如果觉得博主的文章赏心悦目,还请——
点赞
收藏
⭐️评论