介绍

easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员

就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板

官网地址:http://easypoi.mydoc.io/

easypoi需要导入的包

                <!--easypoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>

easypoi工具类


package com.meeno.framework.office.excel.easypoi.utils; import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException; /**
* @description: easypoiUtils
* @author: Wzq
* @create: 2019-11-08 14:54
*/
public class ExcelUtiles { public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,
String fileName, boolean isCreateHeader, HttpServletResponse response){
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
} public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,
HttpServletResponse response){
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
} public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){
defaultExport(list, fileName, response);
} private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName,
HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
if (workbook != null); downLoadExcel(fileName, response, workbook);
} private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
//throw new NormalException(e.getMessage());
}
} private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
if (workbook != null);
downLoadExcel(fileName, response, workbook);
} public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){
if (StringUtils.isBlank(filePath)){
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
}catch (NoSuchElementException e){
//throw new NormalException("模板不能为空");
} catch (Exception e) {
e.printStackTrace();
//throw new NormalException(e.getMessage());
} return list;
} public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
if (file == null){ return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
}catch (NoSuchElementException e){
// throw new NormalException("excel文件不能为空");
} catch (Exception e) {
//throw new NormalException(e.getMessage());
System.out.println(e.getMessage());
}
return list;
}
}

创建导出excel模板的实体类,也可以按照模板导出具体看官网文档

导出excel模板的实体类代码如下:

package com.meeno.framework.office.excel.easypoi.model;

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data; import java.io.Serializable; /**
* @description: 签到记录ExecelModel
* @author: Wzq
* @create: 2020-01-07 11:34
*/
@Data
public class SignRecordExcelModel implements Serializable { /**
* 序号
*/
@Excel(name = "序号",orderNum = "0")
private Integer number; /**
* 人员名称
*/
@Excel(name = "人员名称",orderNum = "1")
private String employeeName; /**
* 座位号
*/
@Excel(name = "座位号",orderNum = "2")
private Integer seatNum; /**
* 签到状态
*/
@Excel(name = "签到状态",orderNum = "3")
private String signStatusStr; }

使用easypoi导出excel

controller层调用代码

/**
*@Description 导出登录记录表
*@Param [session, request, response, data]
*@Return void
*@Author Wzq
*@Date 2020/1/7
*@Time 11:06
*/
@RequestMapping(value = "exportSignRecord.action")
public void exportSignRecord(final HttpSession session,
final HttpServletRequest request,
final HttpServletResponse response,
@RequestParam(value = "Data",required = false) String data){
JSONObject jsonObject = JSONObject.parseObject(data);
//会议id
Long meetingId = jsonObject.getLong("meetingId");
List<SignRecordExcelModel> signRecordExcelModels = this.signRecordService.exportSignRecord(meetingId);
Meeting meeting = this.meetingService.getMeetingDetail(meetingId);
//第一参数:导出的模板类集合
//第二参数:excel中里面内容合并单元格的title
//第三参数:seeetName名称
//第四参数:导出模板实体类的class
//第五参数:导出文件文件名
//第六参数:HttpServletResponse
ExcelUtiles.exportExcel(signRecordExcelModels,"","签到表",SignRecordExcelModel.class,meeting.getName()+"签到表.xlsx",response);
}

导出效果

java导出excel(easypoi)的更多相关文章

  1. java导出excel报错:getOutputStream() has already been called for this response

    对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...

  2. java导出excel表格

    java导出excel表格: 1.导入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifac ...

  3. java导出excel报表

    1.java导出excel报表: package cn.jcenterhome.util; import java.io.OutputStream;import java.util.List;impo ...

  4. Java导出Excel和CSV(简单Demo)

    Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...

  5. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  6. java导出excel模板数据

    Java导出excel数据模板,这里直接贴代码开发,流程性的走下去就是步骤: String[] colName=new String[]{"期间","科目代码" ...

  7. java导出excel工具类

    java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...

  8. [转载]Java导出Excel

    一.需求介绍 当前B/S模式已成为应用开发的主流,而在开发企业办公系统的过程中,常常有客户这样子要求:把系统数据库中的数据导出到Excel,用户查看报表时直接用Excel打开.或者是:用户已经习惯用E ...

  9. Java导出excel

    一.介绍 常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际的开发中,很多时候需要实现导入.导出Excel的应用. ...

随机推荐

  1. postgresql安装及配置

    目录 1. 安装 2. PostgrepSQL的简单配置 2.1 修改监听的ip和端口 2.2 修改数据库log相关的参数 2.3 内存参数 3. 数据库的基础操作 3.1 连接数据库控制台 3.2 ...

  2. 开发必备linux命令大全-稳赚不亏

    我们的服务一般都是在linux系统运行,因此了解一些关于linux命令是必须.接下来将一一详细介绍一些常用的linux的命令 文件操作 远程登录与操作 磁盘挂载 进程管理 启动和结束 系统性能参数查看 ...

  3. JAVA程序系统测试感受

    JAVA课程才刚刚开始,就仿佛经历了一场劫难,让我们叫苦连天,苦不堪言.暑假学的一些皮毛java知识,到了真正需要写一个相对完整的软件系统,就如同废材一样,实在是用不上来.我看着小民哥布置的考试内容, ...

  4. Java基础00-继承17

    1. 继承 1.1 继承概述 但是我们将相同的类提取出来就会变成这个样子 让他们之间产生一个继承的关系 1.2 继承的好处和弊端 IS-A.HAS-A和USE-A关系 苹果是水果的一种可以使用继承猫是 ...

  5. 怀疑前端组件把我的excel文件搞坏了,怎么证明

    背景 我在做个需求,用户通过excel上传文件,文件中,每一行就是一条数据,后台批量处理:但是呢,用户填的数据可能有问题,所以我后台想先做个检查,然后在每一行中加一列,来指出这一行存在的问题. 我本来 ...

  6. POJ3264线段树求最值

    刚开始还觉得有点怪怪的.因为想着如果每个树只是单纯地记录它所在的区间的话会不会有不在区间内的数据给更新了,但是我好像是傻掉了因为如果有这种情况出现的话在父亲节点就会分成l,mid和mid+1,r两个区 ...

  7. 用postman进行web端自动化测试

    概括说一下,web接口自动化测试就是模拟人的操作来进行功能自动化,主要用来跑通业务流程. 主要有两种请求方式:post和get,get请求一般用来查看网页信息:post请求一般用来更改请求参数,查看结 ...

  8. SQL注入之二次,加解密,DNS等注入

    #sql注入之二次注入 1.注入原理 二次注入可以理解为,构造恶意数据存储在数据库后,恶意数据被读取并进入到了SQL查询语句所导致的注入.恶意数据插入到数据库时被处理的数据又被还原并存储在数据库中,当 ...

  9. U 跳转中加入变量参数的写法

    href="{:U('Message/news?id='.$vo['messageid'].'')}" 就是在U方法里如果参数是变量就用 '.$i.'代替 {$i} <a h ...

  10. 学会这十招,轻松搜索github优质项目

    大家好,我是青空. 今天我想给大家分享一下使用 GitHub 的一些心得体会.之前我是在分享 GitHub上的一些开源项目,通过这段时间的收集工作,我积累了一些相关的经验在这里分享给大家. 我做了一个 ...