提示

使用这种方式,会使你的项目打jar包后,体积增加20MB左右(仅为个人测试)

如果你对表格样式、格式没有要求,可以曲线救国,代码看我其他文章

添加pom依赖

        <!--导出excle依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>

实体类

package cn.myauthx.api.main.entity;

import cn.myauthx.api.base.po.baseEntity;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.annotation.write.style.HeadStyle;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors; import java.util.Date; /**
* <p>
*
* </p>
*
* @author DaenMax
* @since 2022-01-06
*/
@Data
@Accessors(chain = true)
@TableName("ma_card")
public class Card extends Model { private static final long serialVersionUID = 1L;
@ExcelProperty(value = "ID",index = 0)
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ExcelProperty(value = "卡密",index = 1)
private String ckey; /**
* 点数
*/
@ExcelProperty(value = "点数",index = 2)
private Integer point; /**
* 秒数
*/
@ExcelProperty(value = "秒数",index = 3)
private Integer seconds; /**
* 生成时间
*/
@ExcelIgnore //表明我在写的时候不用这列
private Integer addTime; /**
* 使用时间
*/
@ExcelIgnore //表明我在写的时候不用这列
private Integer letTime; /**
* 生成时间
*/ @ExcelProperty(value = "生成时间",index = 4)
@TableField(exist = false)
private Date addTimeName; /**
* 使用时间
*/
@ExcelProperty(value = "使用时间",index = 5)
@TableField(exist = false)
private Date letTimeName; /**
* 使用人账号
*/
@ExcelProperty(value = "使用人账号",index = 6)
private String letUser; /**
* 卡密状态,0=未使用,1=已使用,2=被禁用
*/
@ExcelIgnore //表明我在写的时候不用这列
private Integer status; /**
* 卡密状态,0=未使用,1=已使用,2=被禁用
*/
@ExcelProperty(value = "卡密状态",index = 7)
@TableField(exist = false)
private String statusName; /**
* 所属软件id
*/
@ExcelIgnore //表明我在写的时候不用这列
private Integer fromSoftId; /**
* 所属软件名称
*/
@ExcelProperty(value = "所属软件",index = 8)
@TableField(exist = false)
private String fromSoftName; }

工具类

/**
* 导出XLS
*
* @param request
* @param response
* @param fileName 导出的文件名,不需要加.xlsx
* @param sheetName 工作表名
* @param list<T>
* @param entityClass
*/
public static <T> void exportXls(HttpServletRequest request, HttpServletResponse response, String fileName, String sheetName, List<T> list,Class entityClass) {
//因为我项目的特殊性,所以必须先getSession,否则会报错
request.getSession();
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
try {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
ExcelWriterBuilder write = EasyExcel.write(outputStream, entityClass);
ExcelWriterSheetBuilder sheet = write.sheet(sheetName);
sheet.doWrite(list);
return;
}

java springboot api接口使用easyexcle导出xlsx的更多相关文章

  1. 没想到吧,Java开发 API接口可以不用写 Controller了

    本文案例收录在 https://github.com/chengxy-nds/Springboot-Notebook 大家好,我是小富~ 今天介绍我正在用的一款高效敏捷开发工具magic-api,顺便 ...

  2. java web api接口调用

    Web Services 被W3C进行了标准化定义. Web Services 发布到网上,可以公布到某个全局注册表,自动提供服务URL,服务描述.接口调用要求.参数说明以及返回值说明.比如中国气象局 ...

  3. java 调用 api接口

    /* * Copyright 2018 textile.com All right reserved. This software is the * confidential and propriet ...

  4. springBoot api接口

    application/json 请求接口 @RequestMapping(value = "/getBaseData", method = RequestMethod.POST, ...

  5. 使用Layui、Axios、Springboot(Java) 实现EasyExcel的导入导出(浏览器下载)

    实现EasyExcel的导入导出(浏览器下载) 实现三个按钮的功能,但是却花费了一天的时间包括总结. 使用到的技术:springboot layui axios EasyExcel mybatis-p ...

  6. SpringBoot RestFul风格API接口开发

    本文介绍在使用springBoot如何进行Restful Api接口的开发及相关注解已经参数传递如何处理. 一.概念: REST全称是Representational State Transfer,中 ...

  7. SpringBoot实战(二)Restful风格API接口

    在上一篇SpringBoot实战(一)HelloWorld的基础上,编写一个Restful风格的API接口: 1.根据MVC原则,创建一个简单的目录结构,包括controller和entity,分别创 ...

  8. java springboot调用第三方接口 借助hutoool工具类 爬坑

    楼主是个后端小白一枚,之前没接触过后端,只学了java基本语法,还是在学校老师教的,学的很浅,什么ssh.ssm框架都没有学,最近在自学spring boot,看书学也看不是很懂,就在b站上看教学视频 ...

  9. SpringBoot + Swagger2 自动生成API接口文档

    spring-boot作为当前最为流行的Java web开发脚手架,相信越来越多的开发者会使用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于移动端 ...

  10. SpringBoot接口 - API接口有哪些不安全的因素?如何对接口进行签名?

    在以SpringBoot开发后台API接口时,会存在哪些接口不安全的因素呢?通常如何去解决的呢?本文主要介绍API接口有不安全的因素以及常见的保证接口安全的方式,重点实践如何对接口进行签名.@pdai ...

随机推荐

  1. linux怎么判断服务器的cpu架构

    在部署应用程序和服务时,确认服务器的CPU架构是非常重要的,因为这会直接影响软件的兼容性和性能.在Linux系统中,有许多方法可以获取服务器的CPU架构信息.本篇文章将介绍几种常用的方法,并提供代码示 ...

  2. [tldr]GO使用正则表达式

    简述如何使用GO调用正则表达式 是否符合条件 使用MatchString方法实现 _, err := regexp.MatchString(regex, str) 提取内容 Compile 第一步需要 ...

  3. [AI/GPT/综述] AI Agent的设计模式综述

    序:文由 其一,随着大模型的发展,通用智能不断迭代升级,应用模式也不断创新,从简单的Prompt应用.RAG(搜索增强生成).再到AI Agent(人工智能代理). 其中AI Agent一直是个火热的 ...

  4. 更新docker配置,重启docker进程,容器不重启

    前言 想重启 dockerd ,重新加载配置文件,可又怕重启容器,影响线上业务. reload 重新加载配置 dockerd reload 配置,不会重启 dockerd kill -SIGHUP $ ...

  5. gitlab启动与关闭

    gitlab安装完毕后,默认开机启动,开启关闭 sudo gitlab-ctl start/stop

  6. PostgreSQL的可变字符串

    在Oralce中,通常都使用varchar2作为字符串,它能自动删除前后空格.因业务需要用到Postgre,使用了character类型,用起来是没有什么问题.后来发现在smartBI引用这里面的数据 ...

  7. Vmware ESXi 是免费吗?一文弄懂vSphere功能特性及ESXi与vSphere到底有什么区别和联系。

    目录 收起 一.对VMware vSphere及ESXi的相关疑问 1.Vmware vSphere 有些什么功能? 2.ESXi 是否真正免费? 3. ESXi 和 vSphere 到底有什么区别, ...

  8. BandiZip无广告版安装

    BandiZip无广告版安装 Bandizip 是一款压缩软件,它支持Zip.7-Zip 和 RAR 以及其它压缩格式.它拥有非常快速的压缩和解压缩的算法,从大学用到现在,但是现在最新的版本在每次压缩 ...

  9. 一个检查左右括号是否配对的语法检查器(c语言)

    目录 一.题目如下 二.解题思路 三.代码实现 四.测试结果 一.题目如下 通过键盘输入一个包括 '(' 和 ')' 的字符串string ,判断字符串是否有效.要求设计算法实现检查字符串是否有效,有 ...

  10. python API 之 fastapi

    为什么选择 FastAPI? 高性能:基于 Starlette 和 Uvicorn,支持异步请求处理 开发效率:自动交互文档.类型提示.代码自动补全 现代标准:兼容 OpenAPI 和 JSON Sc ...