开发中经常会遇到excel的处理,导入导出解析等等,java中比较流行的用poi,但是每次都要写大段工具类来搞定这事儿,此处推荐一个别人造好的轮子【easypoi】,下面介绍下“轮子”的使用。
pom引入
不再需要其他jar
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.3</version>
</dependency>
编写实体类
此处注意必须要有空构造函数,否则会报错“对象创建错误”
关于注解@Excel,其他还有@ExcelCollection,@ExcelEntity ,@ExcelIgnore,@ExcelTarget等,此处我们用不到,可以去官方查看更多
属性 类型 类型 说明
name String null 列名
needMerge boolean fasle 纵向合并单元格
orderNum String "0" 列的排序,支持name_id
replace String[] {} 值得替换 导出是{a_id,b_id} 导入反过来
savePath String "upload" 导入文件保存路径
type int 1 导出类型 1 是文本 2 是图片,3 是函数,10 是数字 默认是文本
width double 10 列宽
height double 10 列高,后期打算统一使用@ExcelTarget的height,这个会被废弃,注意
isStatistics boolean fasle 自动统计数据,在追加一行统计,把所有数据都和输出这个处理会吞没异常,请注意这一点
isHyperlink boolean false 超链接,如果是需要实现接口返回对象
isImportField boolean true 校验字段,看看这个字段是不是导入的Excel中有,如果没有说明是错误的Excel,读取失败,支持name_id
exportFormat String "" 导出的时间格式,以这个是否为空来判断是否需要格式化日期
importFormat String "" 导入的时间格式,以这个是否为空来判断是否需要格式化日期
format String "" 时间格式,相当于同时设置了exportFormat 和 importFormat
databaseFormat String "yyyyMMddHHmmss" 导出时间设置,如果字段是Date类型则不需要设置 数据库如果是string 类型,这个需要设置这个数据库格式,用以转换时间格式输出
numFormat String "" 数字格式化,参数是Pattern,使用的对象是DecimalFormat
imageType int 1 导出类型 1 从file读取 2 是从数据库中读取 默认是文件 同样导入也是一样的
suffix String "" 文字后缀,如% 90 变成90%
isWrap boolean true 是否换行 即支持\n
mergeRely int[] {} 合并单元格依赖关系,比如第二列合并是基于第一列 则{1}就可以了
mergeVertical boolean fasle 纵向合并内容相同的单元格
import cn.afterturn.easypoi.excel.annotation.Excel;
import java.util.Date;
public class Person {
@Excel(name = "姓名", orderNum = "0")
private String name;
@Excel(name = "性别", replace = {"男_1", "女_2"}, orderNum = "1")
private String sex;
@Excel(name = "生日", exportFormat = "yyyy-MM-dd", orderNum = "2")
private Date birthday;
public Person(String name, String sex, Date birthday) {
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
导入导出公用方法
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());
}
return list;
}
对的,没看错,这就可以导出导入了,看起来代码挺多,其实是提供了多个导入导出方法而已
测试
@RequestMapping("export")
public void export(HttpServletResponse response){
//模拟从数据库获取需要导出的数据
List<Person> personList = new ArrayList<>();
Person person1 = new Person("路飞","1",new Date());
Person person2 = new Person("娜美","2", DateUtils.addDate(new Date(),3));
Person person3 = new Person("索隆","1", DateUtils.addDate(new Date(),10));
Person person4 = new Person("小狸猫","1", DateUtils.addDate(new Date(),-10));
personList.add(person1);
personList.add(person2);
personList.add(person3);
personList.add(person4);
//导出操作
FileUtil.exportExcel(personList,"花名册","草帽一伙",Person.class,"海贼王.xls",response);
}
@RequestMapping("importExcel")
public void importExcel(){
String filePath = "F:\\海贼王.xls";
//解析excel,
List<Person> personList = FileUtil.importExcel(filePath,1,1,Person.class);
//也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
System.out.println("导入数据一共【"+personList.size()+"】行");
//TODO 保存数据库
}
导出结果
导出结果
测试导入
导出结果再添加一行,执行,输出导入数据行数
分享到:
相关推荐
在本项目中,"spring boot+mybatis plus+easy poi实现数据库导出成excel和excel导入到数据库",开发者利用了Spring Boot、MyBatis Plus和EasyPoi库来实现了一个功能强大的数据交互系统,允许用户将数据库中的数据...
总的来说,"SpringBoot + Mybatis + POI导入、导出Excel文件"的项目是一个实用的工具,可以帮助开发者快速实现数据交换功能,提高工作效率。通过理解SpringBoot的自动配置、Mybatis的SQL映射以及POI的Excel操作,...
本篇文章将详细介绍如何利用SpringBoot和MyBatis结合Apache POI实现Excel的导入导出功能。 首先,SpringBoot是一个基于Spring框架的轻量级开发工具,它简化了新Spring应用的初始搭建以及开发过程。SpringBoot通过...
springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式...
总之,"Springboot+Poi实现Excel的导入导出"项目结合了多种强大的工具,提供了一种高效、灵活的数据处理解决方案。无论是开发数据管理应用,还是进行数据分析任务,这样的功能都极具价值。通过深入了解和实践这些...
本文将详细讲解如何在SpringBoot项目中集成easypoi,实现Excel的导入导出功能。 首先,让我们了解easypoi的基本概念。easypoi是基于Apache POI的一个Java库,它简化了POI的API,使得开发者能够更加便捷地操作Excel...
对于导入,你需要读取上传的Excel文件,解析每一行的数据,验证其格式和内容,然后将有效数据存入数据库。 4. **错误处理与验证**:在导入过程中,确保对用户上传的文件进行验证,检查文件类型、大小以及内容是否...
在本项目中,"SpringBootMybatis+poi+Thymeleaf实现excel文件数据导入到数据库以及从数据库将数据导出成excel.zip",我们主要关注的是如何利用Java技术栈来处理Excel文件,并与数据库进行交互。以下是相关知识点的...
SpringBoot+AntDesignVue 实现 Excel 导入功能 在本篇文章中,我们将介绍如何使用 SpringBoot 和 AntDesignVue 实现 Excel 导入功能。这个功能主要是使用 Ant Design Vue 中的 upload 组件来实现导入 excel 文件的...
采用redis存储token及权限信息 报表前端采用B ootstrap框架,结合Jquery Ajax,整合前端Layer.js(提供弹窗)+Bootstrap-table(数据列表展示)+ Bootstrap-Export(各种报表导出SQL,Excel,pdf等)框架,整合Echars...
报表前端采用Bootstrap框架,结合Jquery Ajax,整合前端Layer.js(提供弹窗)+Bootstrap-table(数据列表展示)+ Bootstrap-Export(各种报表导出SQL,Excel,pdf等)框架,整合Echars,各类图表的展示(折线图,饼图...
Easypoi是一个强大的Java操作Excel的工具,支持Excel的读写,特别适用于处理大量数据的导入导出。下面我们将详细阐述这一过程。 首先,我们需要在SpringBoot项目中引入Easypoi的相关依赖。Easypoi提供了注解方式来...
1. 复制、导出与打印:系统提供了数据复制、导出为常见格式(如CSV、Excel)以及打印功能,方便用户进行数据备份、分析和分享。 2. 自动填充:在数据录入过程中,系统能根据已有的数据自动填充相关信息,减少重复...
lombock应用,excel导入导出,swagger2集成,初步集成redis 部分pom.xml <groupId>cn.afterturn</groupId> <artifactId>easypoi-base <version>3.0.3 <!--easypoi-web 耦合了spring-mvc 基于...
资源名字:基于Springboot+vue+mysql的人力资源管理系统设计与实现(源码+文档).zip 资源内容:项目全套源码+完整文档 源码说明: 全部项目源码都是经过测试校正后百分百成功运行。 包含模块: 第4章系统实现 4.1...
springboot项目,基于SpringBoot+Vue实现增删改查分页DEMO(源码+数据库) 技术栈 数据库:MySQL 后端框架:SpringBoot+Spring Data JPA 前端框架:Vue-ElementUI
本文将详细介绍如何使用SpringBoot框架结合EasyExcel和MyBatis-Plus来实现Excel的批量导入和导出,从而提高数据处理效率。 首先,SpringBoot是一个轻量级的Java开发框架,它简化了Spring应用的初始搭建以及开发过程...
它利用Apache POI库操作Excel,可以方便地将数据导出到Excel文件,或者从Excel导入数据。在Spring Boot应用中,Jxls可以无缝集成,帮助我们快速实现复杂的数据导出功能,比如自定义样式、合并单元格等。 Logback是...