通过EasyPOI导入excel数据

前端

html

  <input oninput="uploadStudentList()" style="display: none" id="selectStudentList" type="file" accept="text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">

js

function uploadStudentList(){var file = document.getElementById("selectStudentList").files[0];var formData = new FormData();formData.append("studentList",file)$.ajax({type: "POST",contentType: false,processData: false,url: "/admin/student/importStudentList",data: formData,success:function(data){if(data.code == 0){showSuccessMsg('数据导入成功',function(){$("input[type='checkbox']:checked").parents("tr").remove();})}else{showErrorMsg(data.msg);}},error:function (err){showErrorMsg('网络错误');}})
}

后端

Controller

package com.wehbmu.campus_market.controller.admin;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 org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import com.wehbmu.campus_market.bean.CodeMsg;
import com.wehbmu.campus_market.bean.PageBean;
import com.wehbmu.campus_market.bean.Result;
import com.wehbmu.campus_market.entity.common.Student;
import com.wehbmu.campus_market.service.common.StudentService;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;/*** 后台学生管理控制器* @author Hmoumou**/
@RequestMapping("/admin/student")
@Controller
public class StudentController {@Autowiredprivate StudentService studentService;/*** 学生信息导批量导入操作*/@RequestMapping("/importStudentList")@ResponseBodypublic Result<Boolean> importStudentList(@RequestParam(name = "studentList")MultipartFile file) throws Exception {/*使用easypoi获取文件数据*/ImportParams params = new ImportParams();params.setTitleRows(1);params.setHeadRows(1);/*主键设置,如果该单元格无值则被认为无效数据*/params.setKeyIndex(0);/*获取excel表格中数据,并封装为一个结果对象*/List<Student> students = ExcelImportUtil.importExcel(file.getInputStream(), Student.class, params);students.forEach(student ->{this.studentService.save(student);});return Result.success(true);}}

entity

package com.wehbmu.campus_market.entity.common;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Table;import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelIgnore;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;import com.wehbmu.campus_market.annotion.ValidateEntity;/*** 学生实体类* @author Hmoumou**/
@Entity
@Table(name="vesper_studnet")
@EntityListeners(AuditingEntityListener.class)
@ExcelTarget("students")
public class Student extends BaseEntity{public static final int STUDENT_STATUS_ENABLE = 1;//状态可用public static final int STUDENT_STATUS_UNABLE = 0;//状态不可用/****/private static final long serialVersionUID = 1L;@ValidateEntity(required=true,requiredLeng=true,minLength=6,maxLength=18,errorRequiredMsg="学号不能为空!",errorMinLengthMsg="学号长度需大于6!",errorMaxLengthMsg="学号长度不能大于18!")@Column(name="sn",nullable=false,length=18,unique=true)@Excel(name = "学号",width = 20.0)private String sn;//学生学号@ValidateEntity(required=true,requiredLeng=true,minLength=6,maxLength=18,errorRequiredMsg="密码不能为空!",errorMinLengthMsg="密码长度需大于6!",errorMaxLengthMsg="密码长度不能大于18!")@Column(name="password",nullable=false,length=18)@Excel(name = "密码",width = 20.0)private String password;//学生登录密码@ValidateEntity(required=false)@Column(name="head_pic",length=128)@ExcelIgnoreprivate String headPic;//学生头像/*** 学生证*/@ValidateEntity(required=false)@Column(name="stu_idcard",length=128)@ExcelIgnoreprivate String stuIdcard;@ValidateEntity(required=false)@Column(name="nickname",length=32)@Excel(name = "昵称",width = 20.0)private String nickname;//昵称@ValidateEntity(required=true)@Column(name="mobile",length=18)@Excel(name = "联系方式",width = 20.0)private String mobile;//手机号@ValidateEntity(required=false,minLength=5,maxLength=12,errorMinLengthMsg="qq号最小5位",errorMaxLengthMsg="qq号长度不能大于12")@Column(name="qq",length=18)@Excel(name = "QQ",width = 20.0)private String qq;//QQ号@ValidateEntity(required = true)@Column(name = "email",length = 20)@Excel(name = "邮箱",width = 20.0)private String email;  //QQ邮箱@ValidateEntity(required=false)@Column(name="school",length=18)@Excel(name = "学校",width = 20.0)private String school;//所属学校@ValidateEntity(required=false)@Column(name="academy",length=18)@Excel(name = "学院",width = 20.0)private String academy;//所属学院@ValidateEntity(required=false)@Column(name="grade",length=18)@Excel(name = "年级")private String grade;//所属年级@ValidateEntity(required=false)@Column(name="status",length=1)@Excel(name = "状态",replace = {"激活_1","冻结_0"})private int status = STUDENT_STATUS_ENABLE;//学生状态,默认可用@ValidateEntity(required = false)@Column(name = "ipaddress",length = 20)@Excel(name = "登录地IP",width = 15.0)private String ipaddress;@ValidateEntity(required = false)@Column(name = "regionaddress",length = 20)@Excel(name = "地点",width = 15.0)private String regionaddress;public String getIpaddress() {return ipaddress;}public String getRegionaddress() {return regionaddress;}public void setRegionaddress(String regionaddress) {this.regionaddress = regionaddress;}public void setIpaddress(String ipaddress) {this.ipaddress = ipaddress;}public String getSn() {return sn;}public void setSn(String sn) {this.sn = sn;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}public String getQq() {return qq;}public void setQq(String qq) {this.qq = qq;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getSchool() {return school;}public void setSchool(String school) {this.school = school;}public String getAcademy() {return academy;}public void setAcademy(String academy) {this.academy = academy;}public String getGrade() {return grade;}public void setGrade(String grade) {this.grade = grade;}public String getHeadPic() {return headPic;}public void setHeadPic(String headPic) {this.headPic = headPic;}public String getStuIdcard() {return stuIdcard;}public void setStuIdcard(String stuIdcard) {this.stuIdcard = stuIdcard;}public int getStatus() {return status;}public void setStatus(int status) {this.status = status;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "Student [sn=" + sn + ", password=" + password + ", headPic="+ headPic + ", nickname=" + nickname + ", mobile=" + mobile+ ", qq=" + qq + ", school=" + school + ", academy=" + academy+ ", grade=" + grade + ", status=" + status +",stuIdcard="+stuIdcard+",ipaddress="+ipaddress+",realAddress="+regionaddress+ "]";}
}

实体校验

package com.wehbmu.campus_market.annotion;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 实体检验自定义注解类,根据我们自定义的注解去检查实体各个字段是否在规定的值内* @author Hmoumou**/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidateEntity {public boolean required() default false;//是否检验nullpublic boolean requiredLeng() default false;//是否检验检验长度public boolean requiredMaxValue() default false;//是否检验最大值public boolean requiredMinValue() default false;//是否检验最小值public int maxLength() default -1;//最大长度public int minLength() default -1;//最小长度public long maxValue() default -1;//大值public long minValue() default -1;//最小值public String errorRequiredMsg() default "";//值为null时的错误提示信息public String errorMinLengthMsg() default "";//最小长度不满足时的提示信息public String errorMaxLengthMsg() default "";//最大长度不满足时的提示信息public String errorMinValueMsg() default "";//最小值不满足时的提示信息public String errorMaxValueMsg() default "";//最大值不满足时的提示信息
}

通过EasyPOI导入excel数据相关推荐

  1. python绘制动态图表怎么存下来_用python如何实现导入excel数据后自动生成图表?python如何实现交互式动态图表?...

    这个需求涉及的环节太多了.导入excel文件,获取数据 -- 需要xlrd模块把数据导入python 2. 设定输出图表类型 -- 需要matplot模块.根据数据复杂度,可能需要ETL,那么需要pa ...

  2. python导入excel数据-如何把python中的数据导入excel

    python将数据导入excel的方法:1.在python官网下载xlrd第三方库:2.利用xlrd中的open_workbook函数读入excel文件,即可在python中导入excel数据. 一. ...

  3. oracle 导入Excel数据

    oracle 导入excel数据 CreateTime--2018年1月30日14:58:51 Author:Marydon 通过plsql实现 1.准备工作 Excel中的字段名称,必须和表结构字段 ...

  4. php 导入表格数据,PHPExcel 导入Excel数据的方法

    这篇文章主要介绍了关于PHPExcel 导入Excel数据的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 一:使用composer下载 phpoffice/phpexcel 或者 ...

  5. arcgis导入excel数据_导入Excel数据到ArcGIS属性表的两种实用方法

    导入Excel数据到ArcGIS有两种方法,一种是用ArcMap的加载数据(黄色+号那个):另一种是用ArcCatalog直接转为shp文件,两种方法的原理是一样的. 第一种方法 1.Excel数据: ...

  6. C# 导入excel数据,解决关闭excel后不能释放资源的问题

    C# 导入excel数据,解决关闭excel后不能释放资源的问题 参考文章: (1)C# 导入excel数据,解决关闭excel后不能释放资源的问题 (2)https://www.cnblogs.co ...

  7. 使用SQLyog导入EXCEL数据和合并数据表

    使用SQLyog导入Excel数据 选择 ``高级工具`` >``导入外部数据`` ,选择``开始新工作``,点击``下一步``,如下图所示. 选择数据源类型为excel,在File name中 ...

  8. easypoi导入excel实践方法:

    easypoi导入excel实践方法: https://www.cnblogs.com/vcmq/p/12149673.html

  9. 纯jquery 前端导入 Excel数据 减少服务端压力

    纯jquery 前端导入 Excel数据 减少服务端压力 前提是要导入jq库在这里就不再累述 重要的是导入xlsx.core.min.js 我根据xlsx.core.min.js编写自己的工具类 这里 ...

  10. python导入excel数据到mysql

    python导入excel数据到mysql 使用多线程,目前大概一分钟写入1w条 环境介绍 windows10-x64 python3.6.5-x64 Excel2016 MySql5.7.18 需要 ...

最新文章

  1. UVa10375 Choose and divide
  2. Objective-C中的Category
  3. 一个简单的blog系统(九) 增加标签和标签页面
  4. 研讨会 | “人工智能与行业知识图谱技术实战”研讨会
  5. js 动态生成html(js根据后台返回数据生成html页面中的table标签)(转义字符)
  6. 主席树-----动态开点,不hash
  7. php怎么安装模板_php 模板框架之smarty 的下载和安装
  8. 通过有向图的可达矩阵判断有向图的连通类型
  9. 关于举办2008年注册电气工程师执业资格考试供配电专业(基础)
  10. audacity音轨加伴奏_如何在Audacity中使用Crossfade进行音轨之间的无缝过渡
  11. 【漏洞利用】逻辑漏洞之任意账号密码重置详解
  12. matlab 光斑质心算法,关于激光探测器光斑质心算法硬件的设计
  13. vue cli关闭eslint语法检查
  14. 计算机操作与应用60,60个技巧使您成为计算机高手!
  15. Unity3D射线检测墙面前停止移动
  16. VirtualBox管理工具Vboxmanage
  17. vmware workstation14永久激活密钥
  18. 56个JavaScript 实用工具函数助你提升开发效率!
  19. 使用Dism++备份系统文件并恢复
  20. C3P0 连接池时报 TimeoutException 的解决方法

热门文章

  1. 计算机网络语音传输杂音回音,Win10系统中QQ语音有回音噪音该如何解决?
  2. 程序设计导引2.4——百练2801 填词
  3. 英文标题中的字母大写规则
  4. CameraRaw升级
  5. matlab在三维人体及服装建模上的应用,Matlab在三维人体及服装建模上的应用
  6. 2021-06-08
  7. 冲突声明(conflicting declaration)解决
  8. wps2016向程序发送命令_解决excel弹出“向程序发送命令时出现问题”的方法
  9. 用python设置背景音乐_用Python设置
  10. 生肖android编程,android小程序,根据生日年份计算出生肖