java使用easypoi批量导入excel
java使用easypoi批量导入excel
实体类:
import cn.afterturn.easypoi.excel.annotation.Excel;
public class InputStuEntity {
private Integer id;
@Excel(name = "姓名")
private String name;
@Excel(name = "性别",replace = {"男_1", "女_0"})
private Integer sex;
@Excel(name = "手机号")
private String mobile;
@Excel(name = "身份证号")
private String id_card;
@Excel(name = "高中院校")
private String middleschoolName;
private Integer middleschool;
@Excel(name = "班级")
private String class_name;
@Excel(name = "班主任姓名")
private String teacherName;
private Integer teacher;
@Excel(name = "意向院校")
private String intention_schoolName;
private Integer intention_school;
@Excel(name = "生源地省")
private String provinceidName;
private Integer provinceid;
@Excel(name = "生源地市")
private String cityidName;
private Integer cityid;
@Excel(name = "生源地县")
private String countyidName;
private Integer countyid;
@Excel(name = "考生号")
private String ks_number;
@Excel(name = "学生类型",replace = {"高中_1", "职高_2","中专_3","初中_4","其他_5"})
private Integer stu_type;
@Excel(name = "QQ")
private String qq;
@Excel(name = "微信")
private String weixin;
private Long userId;
@Override
public String toString() {
return "InputStuEntity [id=" + id + ", name=" + name + ", sex=" + sex + ", mobile=" + mobile + ", id_card="
+ id_card + ", middleschoolName=" + middleschoolName + ", middleschool=" + middleschool
+ ", class_name=" + class_name + ", teacherName=" + teacherName + ", teacher=" + teacher
+ ", intention_schoolName=" + intention_schoolName + ", intention_school=" + intention_school
+ ", provinceidName=" + provinceidName + ", provinceid=" + provinceid + ", cityidName=" + cityidName
+ ", cityid=" + cityid + ", countyidName=" + countyidName + ", countyid=" + countyid + ", ks_number="
+ ks_number + ", stu_type=" + stu_type + ", qq=" + qq + ", weixin=" + weixin + ", userId=" + userId
+ "]";
}
public String getCityidName() {
return cityidName;
}
public void setCityidName(String cityidName) {
this.cityidName = cityidName;
}
public Integer getCityid() {
return cityid;
}
public void setCityid(Integer cityid) {
this.cityid = cityid;
}
public String getCountyidName() {
return countyidName;
}
public void setCountyidName(String countyidName) {
this.countyidName = countyidName;
}
public Integer getCountyid() {
return countyid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setCountyid(Integer countyid) {
this.countyid = countyid;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getId_card() {
return id_card;
}
public void setId_card(String id_card) {
this.id_card = id_card;
}
public String getMiddleschoolName() {
return middleschoolName;
}
public void setMiddleschoolName(String middleschoolName) {
this.middleschoolName = middleschoolName;
}
public Integer getMiddleschool() {
return middleschool;
}
public void setMiddleschool(Integer middleschool) {
this.middleschool = middleschool;
}
public String getClass_name() {
return class_name;
}
public void setClass_name(String class_name) {
this.class_name = class_name;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public Integer getTeacher() {
return teacher;
}
public void setTeacher(Integer teacher) {
this.teacher = teacher;
}
public String getIntention_schoolName() {
return intention_schoolName;
}
public void setIntention_schoolName(String intention_schoolName) {
this.intention_schoolName = intention_schoolName;
}
public Integer getIntention_school() {
return intention_school;
}
public void setIntention_school(Integer intention_school) {
this.intention_school = intention_school;
}
public String getProvinceidName() {
return provinceidName;
}
public void setProvinceidName(String provinceidName) {
this.provinceidName = provinceidName;
}
public Integer getProvinceid() {
return provinceid;
}
public void setProvinceid(Integer provinceid) {
this.provinceid = provinceid;
}
public String getKs_number() {
return ks_number;
}
public void setKs_number(String ks_number) {
this.ks_number = ks_number;
}
public Integer getStu_type() {
return stu_type;
}
public void setStu_type(Integer stu_type) {
this.stu_type = stu_type;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getWeixin() {
return weixin;
}
public void setWeixin(String weixin) {
this.weixin = weixin;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
}
控制层转换为对象
/**
* 导入
* @param file
* @return
*/
@ResponseBody
@RequestMapping("import")
public AjaxResult inputStudentMessage(@RequestParam("file")MultipartFile file) {
ImportParams params = new ImportParams();
List<InputStuEntity> list = null;
try {
params.setTitleRows(0);
params.setHeadRows(1);
params.setImportFields(new String[]{"序号", "姓名", "性别", "手机号", "身份证号", "高中院校","班级","班主任姓名","意向院校", "生源地省", "生源地市", "生源地县", "考生号", "学生类型", "QQ", "微信"});
list = ExcelImportUtil.importExcel(file.getInputStream(), InputStuEntity.class, params);
} catch (Exception e) {
e.printStackTrace();
AjaxResult.fail("文件转换异常");
}
try {
Long userId=getUserId();
String m = cjStudentService.inp(list,userId);
if(m==null) {
return AjaxResult.success("导入成功");
}else {
return AjaxResult.fail(m);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return AjaxResult.fail("导入异常");
}
}
List<InputStuEntity> list = null; 这个集合就是转换的对象
fixed
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。