java导出excel(easypoi)
介绍
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)的更多相关文章
- java导出excel报错:getOutputStream() has already been called for this response
对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...
- java导出excel表格
java导出excel表格: 1.导入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifac ...
- java导出excel报表
1.java导出excel报表: package cn.jcenterhome.util; import java.io.OutputStream;import java.util.List;impo ...
- Java导出Excel和CSV(简单Demo)
Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- java导出excel模板数据
Java导出excel数据模板,这里直接贴代码开发,流程性的走下去就是步骤: String[] colName=new String[]{"期间","科目代码" ...
- java导出excel工具类
java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...
- [转载]Java导出Excel
一.需求介绍 当前B/S模式已成为应用开发的主流,而在开发企业办公系统的过程中,常常有客户这样子要求:把系统数据库中的数据导出到Excel,用户查看报表时直接用Excel打开.或者是:用户已经习惯用E ...
- Java导出excel
一.介绍 常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际的开发中,很多时候需要实现导入.导出Excel的应用. ...
随机推荐
- Git错误:unable to access 'https://git.voicegu.com/qa/qa.git/': SSL certificate problem: unable to get local issuer certificate
fatal: unable to access 'https://git.voicegu.com/qa/qa.git/': SSL certificate problem: unable to get ...
- C语言 c++区别
C语言是C89标准,C++是C++99标准的.C89就是在1989年制定的标准,如今最新的是C11和C++11标准.根据不同的标准,它们的功能也会有所不同,但是越新的版本支持的编译器越少
- 剖析:如何用 SwitchUI 5天写一个微信 —— 聊天界面篇
前置资源 GitHub: SwiftUI-WeChatDemo 第零章:用 SwiftUI 五天组装一个微信 - wavky - 博客园 整体结构 UI 部分代码分布如上图所示,App 的主入口类为 ...
- [刘阳Java]_SpringMVC文件上传第1季_第10讲
今天来介绍一个关于SpringMVC框架的文件上传功能.首先我个人感觉SpringMVC框架的文件上传还是要比Struts2框架要好用一些,灵活性更强.因为SpringMVC框架的文件上传有几种不同的 ...
- 【系统配置】Ubuntu和Windons系统安装配置深度学习环境
Ubuntu系统 1.备份 在服务器上整个装系统之前,需要做好一个工作,也就是相关重要数据的备份,这里主要是将固态中的数据备份到机械硬盘或移动硬盘里,可能在备份的过程中会遇到无法写入的问题,是因为文件 ...
- python + pytest基本使用方法(断言)
#pytest 的基本用法# 安装: pip install pytest#在当前目录下运行 : 输入 pytest# 1.断言#功能:用于计算a与b相加的和def add(a,b): return ...
- Leetcode2.两数相加——简洁易懂
> 简洁易懂讲清原理,讲不清你来打我~ 输入两个链表,相同位置相加,进位给下一个位置,输出相加后的链表
Loss函数 题目一:完成computeCost.m function J = computeCost(X, y, theta) %COMPUTECOST Compute cost for linea ...
- Selenium3自动化测试【20】CSS定位元素
CSS 指层叠样式表 (CascadingStyleSheets),CSS一种用来表现HTML或XML等文件样式的计算机语言,其能够灵活的为页面提供丰富样式的风格. CSS使用选择器为页面元素绑定属性 ...