java springboot api接口使用easyexcle导出xlsx
提示
使用这种方式,会使你的项目打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的更多相关文章
- 没想到吧,Java开发 API接口可以不用写 Controller了
本文案例收录在 https://github.com/chengxy-nds/Springboot-Notebook 大家好,我是小富~ 今天介绍我正在用的一款高效敏捷开发工具magic-api,顺便 ...
- java web api接口调用
Web Services 被W3C进行了标准化定义. Web Services 发布到网上,可以公布到某个全局注册表,自动提供服务URL,服务描述.接口调用要求.参数说明以及返回值说明.比如中国气象局 ...
- java 调用 api接口
/* * Copyright 2018 textile.com All right reserved. This software is the * confidential and propriet ...
- springBoot api接口
application/json 请求接口 @RequestMapping(value = "/getBaseData", method = RequestMethod.POST, ...
- 使用Layui、Axios、Springboot(Java) 实现EasyExcel的导入导出(浏览器下载)
实现EasyExcel的导入导出(浏览器下载) 实现三个按钮的功能,但是却花费了一天的时间包括总结. 使用到的技术:springboot layui axios EasyExcel mybatis-p ...
- SpringBoot RestFul风格API接口开发
本文介绍在使用springBoot如何进行Restful Api接口的开发及相关注解已经参数传递如何处理. 一.概念: REST全称是Representational State Transfer,中 ...
- SpringBoot实战(二)Restful风格API接口
在上一篇SpringBoot实战(一)HelloWorld的基础上,编写一个Restful风格的API接口: 1.根据MVC原则,创建一个简单的目录结构,包括controller和entity,分别创 ...
- java springboot调用第三方接口 借助hutoool工具类 爬坑
楼主是个后端小白一枚,之前没接触过后端,只学了java基本语法,还是在学校老师教的,学的很浅,什么ssh.ssm框架都没有学,最近在自学spring boot,看书学也看不是很懂,就在b站上看教学视频 ...
- SpringBoot + Swagger2 自动生成API接口文档
spring-boot作为当前最为流行的Java web开发脚手架,相信越来越多的开发者会使用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于移动端 ...
- SpringBoot接口 - API接口有哪些不安全的因素?如何对接口进行签名?
在以SpringBoot开发后台API接口时,会存在哪些接口不安全的因素呢?通常如何去解决的呢?本文主要介绍API接口有不安全的因素以及常见的保证接口安全的方式,重点实践如何对接口进行签名.@pdai ...
随机推荐
- Web前端入门第1问:英语是否很重要?有哪些前置条件?
HELLO,这里是大熊学习前端开发的入门笔记. 本系列笔记基于 windows 系统. 在入门之前,是否有这样的疑问: 程序员的英语是否很牛?毕竟程序员的代码像天书一样,比如这样: 答案是否定的. 英 ...
- leaflet生成地图封装成jquery插件使用
公司业务里一直都有使用leaflet地图插件来做地图展示.绘图等操作.公司有个项目已经有好几年了,由于项目原因一直在使用,今年由于google 地图 api过期,导致已经使用的地图无法加载.我作为现在 ...
- JMeter 获取 response body 的数据
JMeter 获取 response body 的数据 位置:右键(HTTP Request) - Add - Post Processors - BeanShell PostProcessor im ...
- 开源组件DockerFIle老是Build失败,如何解决
推荐把外网地址替换为国内高速镜像 # 替换源地址 http://dl-cdn.alpinelinux.org/alpine === https://mirrors.aliyun.com/alpine ...
- 原生 JS 实现 HTML 转 Markdown ,(html2md.js 或 html2markdown.js)
之前因为一些需要,需要转换部分 HTML 标签成 markdown 格式,但是不知不觉就完善到一个相对完整的函数. 然后我就封装成了一个文件放在了 github ( https://github.co ...
- windows 本地部署DeepSeek
一:前言: 那么为什么要本地部署,主要就是企业或者个人为了数据安全和防止受限网络等其 数据安全:数据不用上传到外面,在本地处理,不用担心数据泄露,像金融.医疗这些对数据安全要求高的行业特别需要. 功能 ...
- EntityFrameworkCore 中实体的几种配置方法
使用数据注解 实体类通常是在Models目录下,直接在实体类上添加属性注解,比如[Required]/[Key]等. using System.ComponentModel.DataAnnotatio ...
- luaL_ref如何使用
// main.lua中有个全局函数function gf() print("hello world")end// c++中处理void callgf(){ lua_getglob ...
- Sql查询(Select)语句实例
span { color: rgba(255, 0, 0, 1) } Select 结构: 句子结构: Select 列名 [all/distinct] from 表名 where 条件 group ...
- Tryhackme部分翻译学习
Tryhackme部分翻译学习 1.Weaponization WSH 上传txt到桌面 Set shell = WScript.CreateObject("Wscript.Shell&qu ...