超实用easypoi简单模板导出Excel

2023-11-11 10:50

本文主要是介绍超实用easypoi简单模板导出Excel,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概述

本文用esaypoi3.1.0模板导出,简单适用,能运用于大多数业务场景。陈二狗和他的小伙儿伴儿陈雪峰都惊呆了,还不赶紧瞅瞅!

环境

最简单springBoot(只包含web)+MAVEN+IDEA

步骤

1.导入esaypoi3.1.0 依赖

 

    <!-- easypoi简单导出所需要的jar包 start --><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>3.1.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>3.1.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>3.1.0</version></dependency><!-- easypoi简单导出所需要的jar包 end-->

2.@Transient(要用到这个这个注解,所以还要把jpa的启动器引入,引入jpa,就要配置数据库,所以还要把mysql的驱动jar包导入)

 

<!-- 使用@Transient这个注解需要的jar 或者Hibernate 的core包也行--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>

3.yml配置(我的mysql是8.0的所以url上加了useSSL=false)

4.创建测试数据类

留心数据类型
1.Integer类型
2.String类型
3.字典数据:1 食品 2 服装 3 酒水 4 花卉,展示是显示文字
4.布尔(0 假 1真)判断显示文字:如 1 显示是 0 显示否
5.Date日期类型

package com.springboot.aop.entity;import com.fasterxml.jackson.annotation.JsonFormat;import javax.persistence.Transient;
import java.util.Date;/**测试类* @create by 程二狗 on 2018/10/21 0021**/
public class Goods {@Transient//该注解表明只是作数据存储传输,没和表对应(表中没有该字段)//为了生成 1 2 3 ...序列号private Integer order;//序号//商品所属类别展现的文字@Transientprivate String typeName;//格式化的日期@Transientprivate String dateStr;//商品编号,主键(Integer类型的取值)private Integer no;//商品名称(String类型的取值)private String name;//1 食品 2 服装 3 酒水 4 花卉//商品所属类别(Integer类型的取值,对应的数值要转成相应的文字)private Integer type;//商品保质器(测试日期值得获取)private Date shelfLife;//库存是否还有?0 无 1有(测试Integer类型的三目运算)private Integer isHave;//该商品是否经过了审核"0" 未过,"1" 通过(测试String类型的三目运算)private String  isAudit;public Integer getOrder() {return order;}public void setOrder(Integer order) {this.order = order;}public String getTypeName() {return typeName;}public void setTypeName(String typeName) {this.typeName = typeName;}public Integer getNo() {return no;}public void setNo(Integer no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getType() {return type;}public void setType(Integer type) {this.type = type;}public Date getShelfLife() {return shelfLife;}public void setShelfLife(Date shelfLife) {this.shelfLife = shelfLife;}public Integer getIsHave() {return isHave;}public void setIsHave(Integer isHave) {this.isHave = isHave;}public String getIsAudit() {return isAudit;}public void setIsAudit(String isAudit) {this.isAudit = isAudit;}public String getDateStr() {return dateStr;}public void setDateStr(String dateStr) {this.dateStr = dateStr;}public Goods(Integer no, String name, Integer type, Date shelfLife, Integer isHave, String isAudit) {this.no = no;this.name = name;this.type = type;this.shelfLife = shelfLife;this.isHave = isHave;this.isAudit = isAudit;}@Overridepublic String toString() {return "Goods{" +"order=" + order +", typeName='" + typeName + '\'' +", dateStr='" + dateStr + '\'' +", no=" + no +", name='" + name + '\'' +", isHave=" + isHave +", isAudit='" + isAudit + '\'' +'}';}
}

4.导出Excel代码(核心)

package com.springboot.aop.easypoi;import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.springboot.aop.entity.Goods;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;/*** esayPOI 简单模板导出测试** @create by 程二狗 on 2018/10/21 0021**/@RestController
public class EasyPOIController {@GetMapping("/export")public void export(HttpServletResponse response) {Goods goods1 = new Goods(110, "苹果", 1, new Date(), 0, "1");Goods goods2 = new Goods(111, "格子衫", 2, new Date(), 0, "0");Goods goods3 = new Goods(112, "拉菲红酒", 3, new Date(), 1, "1");Goods goods4 = new Goods(113, "玫瑰", 4, new Date(), 1, "0");List<Goods> goodsList = new ArrayList<>();goodsList.add(goods1);goodsList.add(goods2);goodsList.add(goods3);goodsList.add(goods4);//可以抽取为日期工具类Date date1 = new Date();SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");String date = df.format(date1);for (int i = 0; i < goodsList.size(); ++i) {//添加序号列goodsList.get(i).setOrder(i + 1);//Date类型日期转换goodsList.get(i).setDateStr(df.format(goodsList.get(i).getShelfLife()));//type转换成显示文字if (goodsList.get(i).getType() == 1) {goodsList.get(i).setTypeName("食品");} else if (goodsList.get(i).getType() == 2) {goodsList.get(i).setTypeName("服装");} else if (goodsList.get(i).getType() == 3) {goodsList.get(i).setTypeName("酒水");} else if (goodsList.get(i).getType() == 4) {goodsList.get(i).setTypeName("花卉");}}for (Goods goods : goodsList) {System.out.println(goods);}// 获取导出excel指定模版,第二个参数true代表显示一个Excel中的所有 sheetTemplateExportParams params = new TemplateExportParams("/templates/商品详情表.xls", true);Map<String, Object> data = new HashMap<String, Object>();data.put("date", date);//导出一般都要日期data.put("one", goods1);//导出一个对象data.put("list", goodsList);//导出list集合try {// 简单模板导出方法Workbook book = ExcelExportUtil.exportExcel(params, data);//下载方法export(response, book, "商品信息");} catch (Exception e) {e.printStackTrace();}}/*** export导出请求头设置** @param response* @param workbook* @param fileName* @throws Exception*/private static void export(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {response.reset();response.setContentType("application/x-msdownload");fileName = fileName + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"), "ISO-8859-1") + ".xls");ServletOutputStream outStream = null;try {outStream = response.getOutputStream();workbook.write(outStream);} finally {outStream.close();}}
}

5.构建模板Excel(超级核心),错误经常都是在这儿抛出的

单个对象


list集合

 

小tpis:在实际开发中,我们一般不会去动实体类(该类与数据库表字段一一映射),而是建一个VO或DTO去继承该类,然后在里面进行类的扩展

 

激动人心的时刻

在浏览器中输入请求接口url:http://localhost:8080/export


一个对象效果


list效果

 

总结:
1.String、Integer、Byte类型的可以直接获取,Date类型的必须格式化
2.字典数据的必须代码处理转成相应的文字
3.简单的0 1 的可以用三目运算直接在表格中去转换成相应的文字

再次提醒!!!!!

1.千万别去合并单元格,除非是已知的内容(自己写的)
2.设置样式后,所设置的样式行数一定要大于集合的长度
99.99%出错的人都在这是去合并单元格了的

 

作者:程er狗
链接:https://www.jianshu.com/p/b9ec05479de3
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

这篇关于超实用easypoi简单模板导出Excel的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/389542

相关文章

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 10130 简单背包

题意: 背包和 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc