Excel下载接口

news2024/10/6 6:48:52

在这里插入图片描述

💗wei_shuo的个人主页

💫wei_shuo的学习社区

🌐Hello World !


Excel下载接口

在这里插入图片描述

需求分析

  • 页面表格的数据下载,保存到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<MerchantSettleBalance> 类型的数据列表 list

list.stream().map(……).collect(Collectors.toList()):使用流式操作对 list 进行转换,将每个 MerchantSettleBalance 对象映射为一个 BalanceDownload 对象;使用 Collectors.toList() 方法将转换后的结果收集为一个 List<BalanceDownload> 对象,并将其返回

    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接口调试

在这里插入图片描述

在这里插入图片描述


🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞👍收藏⭐️评论📝


在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1445019.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【HTTP】localhost和127.0.0.1的区别是什么?

目录 localhost是什么呢&#xff1f; 从域名到程序 localhost和127.0.0.1的区别是什么&#xff1f; 域名的等级划分 多网站共用一个IP和端口 私有IP地址 IPv6 今天在网上逛的时候看到一个问题&#xff0c;没想到大家讨论的很热烈&#xff0c;就是标题中这个&#xff1a; …

python常用的深度学习框架

目录 一&#xff1a;介绍 二&#xff1a;使用 Python中有几个非常受欢迎的深度学习框架&#xff0c;它们提供了构建和训练神经网络所需的各种工具和库。以下是一些最常用的Python深度学习框架&#xff1a; 一&#xff1a;介绍 TensorFlow&#xff1a;由Google开发的TensorF…

LeetCode Python -8.字符串转整数

文章目录 题目答案运行结果 题目 请你来实现一个 myAtoi(string s) 函数&#xff0c;使其能将字符串转换成一个 32 位有符号整数&#xff08;类似 C/C 中的 atoi 函数&#xff09;。 函数 myAtoi(string s) 的算法如下&#xff1a; 读入字符串并丢弃无用的前导空格检查下一个…

C语言--------数据在内存中的存储

1.整数在内存中的存储 整数在内存是以补码的形式存在的&#xff1b; 整型家族包括char,int ,long long,short类型&#xff1b; 因为char类型是以ASCII值形式存在&#xff0c;所以也是整形家族&#xff1b; 这四种都包括signed,unsigned两种&#xff0c;即有符号和无符号&am…

ncc匹配提速总结

我们ncc最原始的匹配方法是&#xff1a;学习模板w*h个像素都要带入ncc公式计算 第一种提速&#xff0c;学习模板是w*h&#xff0c;而我们支取其中的w/2*h/2,匹配窗口同理&#xff0c;计算量只有1/4。 另外一种因为ncc是线性匹配&#xff0c;我们在这上面也做了文章&#xff0…

【漏洞复现】狮子鱼CMS文件上传漏洞(wxapp.php)

Nx01 产品简介 狮子鱼CMS&#xff08;Content Management System&#xff09;是一种网站管理系统&#xff0c;它旨在帮助用户更轻松地创建和管理网站。该系统拥有用户友好的界面和丰富的功能&#xff0c;包括页面管理、博客、新闻、产品展示等。通过简单直观的管理界面&#xf…

【51单片机】串口通信实验(包括波特率如何计算)

目录 串口通信实验通信的基本概念串行通信与并行通信异步通信与同步通信单工、 半双工与全双工通信通信速率 51单片机串口介绍串口介绍串口通信简介串口相关寄存器串口工作方式方式0方式1方式 2 和方式 3 串口的使用方法&#xff08;计算波特率&#xff09; 硬件设计软件设计1、…

JAVA设计模式之访问模式详解

访问者模式 1 访问者模式介绍 访问者模式在实际开发中使用的非常少,因为它比较难以实现并且应用该模式肯能会导致代码的可读性变差,可维护性变差,在没有特别必要的情况下,不建议使用访问者模式. 访问者模式(Visitor Pattern) 的原始定义是&#xff1a;允许在运行时将一个或多…

华为 huawei 交换机 接口 MAC 地址学习限制接入用户数量 配置示例

目录 组网需求: 配置思路&#xff1a; 操作步骤&#xff1a; 配置文件&#xff1a; 组网需求: 如 图 2-14 所示&#xff0c;用户网络 1 和用户网络 2 通过 LSW 与 Switch 相连&#xff0c; Switch 连接 LSW 的接口为GE0/0/1 。用户网络 1 和用户网络 2 分别属于 VLAN10 和 V…

第三节 zookeeper基础应用与实战2

目录 1. Watch事件监听 1.1 一次性监听方式&#xff1a;Watcher 1.2 Curator事件监听机制 2. 事务&异步操作演示 2.1 事务演示 2.2 异步操作 3. Zookeeper权限控制 3.1 zk权限控制介绍 3.2 Scheme 权限模式 3.3 ID 授权对象 3.4 Permission权限类型 3.5 在控制台…

antdpro框架npm install 报错,切换tyarn安装成功。

报错日志 有时间补 当前版本 解决办法 进入工作目录 安装官方推荐的tyarn工具&#xff1a;npm install yarn tyarn -g 进行依赖安装&#xff1a;tyarn 启动项目 &#xff1a;tyarn start 注意&#xff1a; 技术迭代较快&#xff0c;建议查询官网后实践&#xff0c;以上作为…

Hive窗口函数详解

一、 窗口函数知识点 1.1 窗户函数的定义 窗口函数可以拆分为【窗口函数】。窗口函数官网指路&#xff1a; LanguageManual WindowingAndAnalytics - Apache Hive - Apache Software Foundationhttps://cwiki.apache.org/confluence/display/Hive/LanguageManual%20Windowing…

并行计算导论 笔记 1

目录 并行编程平台隐式并行超标量执行/指令流水线超长指令字处理器 VLIW 内存性能系统的局限避免内存延迟的方法 并行计算平台控制结构通信模型共享地址空间平台消息传递平台对比 物理组织理想并行计算机并行计算机互联网络网络拓朴结构基于总线的网络交叉开关网络多级网络全连…

微服务架构RabbitMQ实现CQRS模式

在现代软件开发中,微服务架构和CQRS模式都是备受关注的技术趋势。微服务架构通过将应用程序拆分为一系列小型、自治的服务,提供了更好的可伸缩性和灵活性。而CQRS模式则通过将读操作和写操作分离,优化了系统的性能和可维护性。本文小编将为大家介绍如何在ASP.NET Core微服务…

树莓派4B(Raspberry Pi 4B)使用docker搭建阿里巴巴sentinel服务

树莓派4B&#xff08;Raspberry Pi 4B&#xff09;使用docker搭建阿里巴巴sentinel服务 由于国内访问不了docker hub&#xff0c;而国内镜像仓库又没有适配树莓派ARM架构的sentinel镜像&#xff0c;所以我们只能退而求其次——自己动手构建镜像。本文基于Ubuntu&#xff0c;Jav…

Netty应用(八) 之 ByteBuf 半包粘包问题 半包粘包解决方案-封帧解码器

目录 19.ByteBuf 19.1 ByteBuf的基本使用 19.2 ByteBuf的扩容机制 19.3 ByteBuf与内存的关系 19.4 ByteBuf的内存结构 19.5 ByteBuf的API 19.5.1 ByteBuf的写操作 19.5.2 ByteBuf的读操作 19.5.3 ByteBuf的slice 19.6 ByteBuf的内存释放 19.6.1 实现API 19.6.2 如何…

前沿重器[42] | self-RAG-大模型决策的典型案例探究

前沿重器 栏目主要给大家分享各种大厂、顶会的论文和分享&#xff0c;从中抽取关键精华的部分和大家分享&#xff0c;和大家一起把握前沿技术。具体介绍&#xff1a;仓颉专项&#xff1a;飞机大炮我都会&#xff0c;利器心法我还有。&#xff08;算起来&#xff0c;专项启动已经…

Junit5基础教程

文章目录 一&#xff0c;导入依赖二&#xff0c;基本功能一、常用断言二、执行顺序和常用注解1、通过BeforeAll类的注解来保证顺序2、通过order注解来保证执行顺序 三、依赖测试四、参数化测试五、测试套件SelectPackages、IncludePackages、SelectClasses、IncludeTags等注解的…

javaweb物业管理系统jsp项目

文章目录 物业管理系统一、系统演示二、项目介绍三、系统部分功能截图四、部分代码展示五、底部获取项目源码&#xff08;9.9&#xffe5;带走&#xff09; 物业管理系统 可用作javaweb项目、servlet项目、jsp项目的项目设计 一、系统演示 物业管理系统 二、项目介绍 语言&a…

春晚魔术和约瑟夫问题

春晚的魔术实际上是一个约瑟夫问题&#xff0c;最终的结果是魔术开始时确定的几个变量确定好的&#xff0c;扑克牌只是道具和障眼法。网上一查这个问题发现颇有历史渊源&#xff0c;17世纪的法国数学家加斯帕在《数目的游戏问题》中讲了这样一个故事&#xff1a;15个教徒和15 个…