提示

使用这种方式,会使你的项目打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. 通用C++ Makefile

    点击查看代码 ///////////////////////////////////////////// CC = g++ CFLAGS=-Wall -O2 TARGET = main SRCS := ...

  2. 针对N=p^rq分解之初探

    针对N=p^r*q分解之初探 论文地址:https://eprint.iacr.org/2015/399.pdf 题目:https://www.nssctf.cn/problem/2016 from ...

  3. 多机器的键鼠互通——Synergy/Deskflow配置记录

    Synergy (1.14.6) 情况一样,那么感觉就是机器之间TCP连接有问题,测试不同 一些测试命令 ss -tlnp | grep 24800 # 查看端口情况 sudo lsof -i :24 ...

  4. ubuntu 22.04 deskop 无法打开terminal

    系统语言设置的问题,改为汉语即可

  5. Centos安装完成后,ifconfig:command not found

    1.问题:在刚最小安装完centos7,想查看本机IP地址.然后运行ifconfig,结果弹出报错. 2.问题排查:首先我们了解是不是没有开启网卡导致的?我们可以通过一下3种方法来排查: 1)通过pi ...

  6. Laravel MongoDB

    Laravel MongoDB This package adds functionalities to the Eloquent model and Query builder for MongoD ...

  7. 四大AI编程工具组合测评

     在当今数字化浪潮中,AI 编程工具如雨后春笋般涌现,极大地提升了编程效率与体验.本文将详细剖析四类 AI 编程工具组合,从开发工具.大模型.插件搭配,到编程能力.费用体系及综合评价,为开发者提供全面 ...

  8. git和github的入门操作

    之前因为工作中用的都是SVN版本控制工具,没接触过git和github,现在开始深入自学Django框架技术后,看到官网推荐使用git,然后这两天网上查阅了很多文章教程,学到入门操作需要学习的点,太多 ...

  9. vmware vsphere 6.5

    vmware vsphere 6.5是vsphere软件的经典版本,也是一款业界领先的服务器虚拟化平台,作为基础平台,是任何云计算环境的理想之选,其组件包括vCenter Server.ESXi.vS ...

  10. Python 生成器说明

    生成器 python 生成器 常规形态 # list def square_numbers(nums: list): squared_nums = [] for i in nums: squared_ ...