SpringBoot2.x使用EasyPOI导入Excel浅谈

平时经常遇到客户要帮忙导入一些数据到数据库中,有些数据比较多有时候手动录入就会很耗时间,所以就自己写一个Excel导入的demo记录一下我对EasyPOI的误区;本文使用SpringBoot2.0,EasyPOI

开发框架

框架:SpringBoot2.0
java jdk 1.8
开发工具:Eclipse
数据库:Orcal

一.首先在pom.xml中导入EasyPOI的架包

pom.xml的主要文件信息如下:

<!-- easypoi -->
<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>

二.然后编写Controller

Controller:

/**
* @author lr
* @date 2018年12月26日 下午6:51:43
* @version V1.0.0
*/
package com.louis.sql.tools.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import com.louis.sql.tools.model.LongHuaAreaDO;
import com.louis.sql.tools.result.HttpResult;
import com.louis.sql.tools.service.LongHuaAreaService; @RestController
@RequestMapping("longhua")
public class LongHuaController { @Autowired
private LongHuaAreaService longHuaAreaService;
/**
* @Title: listAllData
* @Description: 查询处所有的数据并展示
* @param @return 参数说明
* @return HttpResult 返回类型
* @throws
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
public HttpResult listAllData(){
return HttpResult.ok(longHuaAreaService.listAll());
} @RequestMapping(value = "/deleteByName", method = RequestMethod.DELETE)
public HttpResult deleteByName(@RequestBody List<LongHuaAreaDO> records) { return HttpResult.ok(longHuaAreaService.deleteByName(records));
} /**
* @Title: importExcel
* @Description: 导入Excel方法
* @param @param file 参数说明
* @return void 返回类型
* @throws
*/
@PostMapping("/importExcel")
public void importExcel(@RequestParam("file") MultipartFile file) {
String fileName = file.getOriginalFilename();
try {
longHuaAreaService.batchImport(fileName, file);
} catch (Exception e) {
e.printStackTrace();
}
}
}

三.接口LongHuaAreaService

/**
* @author lr
* @date 2018年12月26日 下午9:26:02
* @version V1.0.0
*/
package com.louis.sql.tools.service; import java.io.IOException;
import java.util.List; import org.springframework.web.multipart.MultipartFile; import com.louis.sql.tools.model.LongHuaAreaDO; public interface LongHuaAreaService { /**
* @Title: listAll
* @Description: 查询所有的龙华区监管人员
* @param @return 参数说明
* @return String 返回类型
* @throws
*/
List<LongHuaAreaDO> listAll(); /**
* @Title: deleteByName
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param records
* @param @return 参数说明
* @return String 返回类型
* @throws
*/
int deleteByName(List<LongHuaAreaDO> records); /**
* @Title: delete
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param record
* @param @return 参数说明
* @return int 返回类型
* @throws
*/
int delete(LongHuaAreaDO record); /**
* @Title: batchImport
* @Description: 导入Excel中的数据
* @param @param fileName
* @param @param file 参数说明
* @return void 返回类型
* @throws
*/
void batchImport(String fileName, MultipartFile file) throws IOException, Exception; }

四.实现方法LongHuaAreaServiceImpl

/**
* @author lr
* @date 2018年12月26日 下午9:26:50
* @version V1.0.0
*/
package com.louis.sql.tools.service.impl; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import com.louis.sql.tools.dao.LongHuaAreaMapper;
import com.louis.sql.tools.model.LongHuaAreaDO;
import com.louis.sql.tools.service.LongHuaAreaService; import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams; @Service
public class LongHuaAreaServiceImpl implements LongHuaAreaService{ @Autowired
private LongHuaAreaMapper longHuaAreaMapper; @Override
public List<LongHuaAreaDO> listAll() { return longHuaAreaMapper.listAll();
} @Override
public int delete(LongHuaAreaDO record) {
return longHuaAreaMapper.deleteByName(record.getName());
} @Override
public int deleteByName(List<LongHuaAreaDO> records) {
for (LongHuaAreaDO record : records) {
delete(record);
}
return 1;
} /*
* Title: batchImport
*Description:
* @param fileName
* @param file
* @see com.louis.sql.tools.service.LongHuaAreaService#batchImport(java.lang.String, org.springframework.web.multipart.MultipartFile)
*/
@Transactional(readOnly = false,rollbackFor = Exception.class)
@Override
public void batchImport(String fileName, MultipartFile file) throws IOException, Exception {
ImportParams params = new ImportParams();
params.setTitleRows(0);
params.setHeadRows(1);
List<LongHuaAreaDO> list = ExcelImportUtil.importExcel(file.getInputStream(), LongHuaAreaDO.class, params);
for (int i = 0; i <list.size() ; i++) {
System.out.println(list.get(i));
System.out.println(list.get(i).getId());
} } }

五.实体类方法LongHuaAreaDO

/**
* @author lr
* @date 2018年12月26日 下午6:32:48
* @version V1.0.0
*/
package com.louis.sql.tools.model; import java.io.Serializable;
import java.util.Date; import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget; @ExcelTarget("BZ_JCRY")
public class LongHuaAreaDO implements Serializable{ private static final long serialVersionUID = -4277023292843262708L; @Excel(name = "ID", orderNum = "0")
private String id;
@Excel(name = "编号", orderNum = "1")
private String cerNo;
@Excel(name = "姓名", orderNum = "2")
private String legalNo;
@Excel(name = "姓名", orderNum = "3")
private String name;
@Excel(name = "性别", replace = {"男_0", "女_1"}, orderNum = "4")
private String sex;
@Excel(name = "手机号", orderNum = "5")
private String mobTel;
@Excel(name = "单位名称", orderNum = "6")
private String corpName;
@Excel(name = "部门名称", orderNum = "7")
private String deptName;
@Excel(name = "职位", orderNum = "8")
private String post;
@Excel(name = "专门", orderNum = "9")
private String expertise;
@Excel(name = "组织机构", orderNum = "10")
private String regOrg;
@Excel(name = "组织机构简称", orderNum = "11")
private String bizOrg;
@Excel(name = "更新时间", exportFormat = "yyyy-MM-dd HH:mm:ss", orderNum = "12")
private Date updateTime;
@Excel(name = "办公室名称", orderNum = "13")
private String officeName;
@Excel(name = "单位编码", orderNum = "14")
private String corpCode;
@Excel(name = "部门编码", orderNum = "15")
private String deptCode;
@Excel(name = "创建时间", exportFormat = "yyyy-MM-dd HH:mm:ss", orderNum = "16")
private Date createTime;
@Excel(name = "雇佣编号", orderNum = "17")
private String employeeCode;
@Excel(name = "是否在职", orderNum = "18")
private String isextract; @Override
public String toString() {
return "LongHuaAreaDO [id=" + id + ", cerNo=" + cerNo + ", legalNo=" + legalNo + ", name=" + name
+ ", sex=" + sex + ", mobTel=" + mobTel + ", corpName=" + corpName + ", deptName=" + deptName
+ ", post=" + post + ", expertise=" + expertise + ", regOrg=" + regOrg + ", bizOrg=" + bizOrg
+ ", updateTime=" + updateTime + ", officeName=" + officeName + ", corpCode=" + corpCode
+ ", deptCode=" + deptCode + ", createTime=" + createTime + ", employeeCode=" + employeeCode
+ ", isextract=" + isextract + "]";
} public String getId() {
return id;
} public void setId(String id) {
this.id = id == null ? null : id.trim();
} public String getCerNo() {
return cerNo;
} public void setCerNo(String cerNo) {
this.cerNo = cerNo == null ? null : cerNo.trim();
} public String getLegalNo() {
return legalNo;
} public void setLegalNo(String legalNo) {
this.legalNo = legalNo == null ? null : legalNo.trim();
} public String getName() {
return name;
} public void setName(String name) {
this.name = name == null ? null : name.trim();
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
} public String getMobTel() {
return mobTel;
} public void setMobTel(String mobTel) {
this.mobTel = mobTel == null ? null : mobTel.trim();
} public String getCorpName() {
return corpName;
} public void setCorpName(String corpName) {
this.corpName = corpName == null ? null : corpName.trim();
} public String getDeptName() {
return deptName;
} public void setDeptName(String deptName) {
this.deptName = deptName == null ? null : deptName.trim();
} public String getPost() {
return post;
} public void setPost(String post) {
this.post = post == null ? null : post.trim();
} public String getExpertise() {
return expertise;
} public void setExpertise(String expertise) {
this.expertise = expertise == null ? null : expertise.trim();
} public String getRegOrg() {
return regOrg;
} public void setRegOrg(String regOrg) {
this.regOrg = regOrg == null ? null : regOrg.trim();
} public String getBizOrg() {
return bizOrg;
} public void setBizOrg(String bizOrg) {
this.bizOrg = bizOrg == null ? null : bizOrg.trim();
} public Date getUpdateTime() {
return updateTime;
} public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
} public String getOfficeName() {
return officeName;
} public void setOfficeName(String officeName) {
this.officeName = officeName == null ? null : officeName.trim();
} public String getCorpCode() {
return corpCode;
} public void setCorpCode(String corpCode) {
this.corpCode = corpCode == null ? null : corpCode.trim();
} public String getDeptCode() {
return deptCode;
} public void setDeptCode(String deptCode) {
this.deptCode = deptCode == null ? null : deptCode.trim();
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} public String getEmployeeCode() {
return employeeCode;
} public void setEmployeeCode(String employeeCode) {
this.employeeCode = employeeCode == null ? null : employeeCode.trim();
} public String getIsextract() {
return isextract;
} public void setIsextract(String isextract) {
this.isextract = isextract == null ? null : isextract.trim();
} }

注意:一定要有toString构造方法

六.要导入的Excel中的内容格式如下:

七.使用Swagger-ui的的接口方法进行导入

八.导入后的效果如下

九.Demo下载

下载地址:https://github.com/PlayTaoist/SpringBoot2-FastDFS

目前只写到从Excel中读取到了数据,后续如何插入到数据库中大家可以自己完善一下

=======================================================================================

博客地址:https://www.codepeople.cn

=======================================================================================

SpringBoot2.x使用EasyPOI导入Excel浅谈的更多相关文章

  1. EasyPoi导入Excel

    EasyPoi的导出Excel功能和导入功能同样简单.我之前强调过,EasyPoi的原理本质就是Poi,正如MyBatis Plus的本质原理就是MyBatis. POI导入功能可以参考如下地址:ht ...

  2. PL/SQL数据导入导出浅谈(1)

    近来需要通过PL/SQL向Oracle中导数据,特此总结一下 试例表:test 字段:id;name;org; 1.直接复制粘贴(当数据量不是特别大的时候) 1)使用select * from tes ...

  3. 浅谈Excel开发:十一 针对64位Excel的插件的开发和部署

    自Office 2010版本开始有了32位和64位之分,对Excel来说,32位的Excel和64位的Excel在性能上的主要区别是64位的Excel能够处理2G及2G以上的大数据集. 随着64位操作 ...

  4. VSTO学习笔记(九)浅谈Excel内容比较

    原文:VSTO学习笔记(九)浅谈Excel内容比较 说起文件内容比较,或许我们首先想到的是UltraCompare这类专业比较的软件,其功能非常强大,能够对基于文本的文件内容作出快速.准确的比较,有详 ...

  5. 浅谈_IDEA导入Eclipse的Web项目

    相信很多同学在工作中都会遇到将一个Eclipse的Web项目导入IDEA的情景,这里浅谈一下具体的操作流程 一:Import Project,选择要导入的项目 二:选择以Eclipse模型的方式导入 ...

  6. EasyPOI导入导出Excel

    EasyPOI工具可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 导入maven依赖 <dependency> <group ...

  7. Easypoi实现excel多sheet表导入导出功能

    Easypoi简化了开发中对文档的导入导出实现,并不像poi那样都要写大段工具类来搞定文档的读写. 第一步引入Easypoi依赖 <!-- 导出文件工具 EasyPoi实现Excel读写管理测试 ...

  8. 【ASP.NET MVC系列】浅谈jqGrid 在ASP.NET MVC中增删改查

    ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...

  9. 浅谈angular2+ionic2

    浅谈angular2+ionic2   前言: 不要用angular的语法去写angular2,有人说二者就像Java和JavaScript的区别.   1. 项目所用:angular2+ionic2 ...

随机推荐

  1. IDEA控制台问题:At least one JAR was scanned for TLDs yet contained no TLD

    参考连接: https://www.cnblogs.com/interdrp/p/7763040.html 1.调整Tomcat对应类的log级别 2.观察Tomcat日志打印信息 3.调整${tom ...

  2. Hive| 压缩| 存储| 调优

    Hadoop压缩配置 修改Hadoop集群具有Snappy压缩方式: 查看hadoop支持的压缩方式 [kris@hadoop101 datas]$ hadoop checknative 将编译好的支 ...

  3. 关于go get安装git golang项目时报错的处理办法

    关于go get安装git golang项目时报错的处理办法 使用go get安装github上的项目时一般来说,不可避免会出错.各种错误的处理办法: 必须条件: 1.安装git并配置环境变量.下载地 ...

  4. web服务-1、http协议的三次握手四次挥手

    知识点:http协议:它是基于tcp协议的,浏览器访问服务器,服务器把资源回给浏览器,这个过程都是遵循http协议的,否则无法完成,http早些年是1.0版本,现在基本上都是1.1版本了,俩个版本的区 ...

  5. YOLO系列:YOLO v2深度解析 v1 vs v2

    概述 第一,在保持原有速度的优势之下,精度上得以提升.VOC 2007数据集测试,67FPS下mAP达到76.8%,40FPS下mAP达到78.6%,可以与Faster R-CNN和SSD一战 第二, ...

  6. 2019-1-11 SQL语句汇总——聚合函数、分组、子查询及组合查询

  7. BZOJ.3591.最长上升子序列(状压DP)

    BZOJ 题意:给出\(1\sim n\)的一个排列的一个最长上升子序列,求原排列可能的种类数. \(n\leq 15\). \(n\)很小,参照HDU 4352这道题,我们直接把求\(LIS\)时的 ...

  8. 机器学习系列-tensorflow-02-基本操作运算

    tensorflow常数操作 import tensorflow as tf # 定义两个常数,a和b a = tf.constant(2) b = tf.constant(3) # 执行默认图运算 ...

  9. HQL实用技术

    HQL是Hibernate Query Language的缩写,提供更加丰富灵活.更为强大的查询能力:HQL更接近SQL语句查询语法. HQL基础查询  1.获取部分列 多列 /** * 获取部分列 ...

  10. [COCI2013]DLAKAVAC

    [COCI2013]DLAKAVAC 题目大意: 有一个长度为\(m(m\le1500)\)的\(01\)串\(A\),进行\(k(k\le10^{18})\)次操作.一次操作完的串中若\(A_i=1 ...