使用的是easypoi,官网文档:http://easypoi.mydoc.io/
/**
* 导入Excel文件
*/
@PostMapping("/importTeacher")
@ResponseBody
public RestResponse importTeacher(@RequestParam MultipartFile file) {
System.out.println("进入1");
ImportParams params = new ImportParams();
//设置excel表的标题
params.setTitleRows(1);
//设置excel字段的名字
params.setHeadRows(1);
List<TeacherDto> list = null; try {
list = ExcelImportUtil.importExcel(file.getInputStream(), TeacherDto.class, params);
} catch (Exception e) {
e.printStackTrace();
return RestResponse.failure("导入文件异常");
} if (!list.isEmpty()){
System.out.println("进入2");
TeacherDao teacherDao=new TeacherDao();
//插入数据
teacherDao.insert(list); }else {
return RestResponse.failure("文件没有数据,导入失败");
}
return RestResponse.success("导入成功");
}
@GetMapping("/exportTeacher")
public void exportTeacher(HttpServletResponse response) throws IOException {
TeacherDao teacherDao=new TeacherDao();
List<TeacherDto> list=teacherDao.export(); ExportParams params = new ExportParams("teacher","teacher");
Workbook workbook = ExcelExportUtil.exportBigExcel(params, TeacherDto.class, list);
ExcelExportUtil.closeExportBigExcel();
//文件命名
SimpleDateFormat df = new SimpleDateFormat("MMddHHmmss");
String date = df.format(new Date());
String filename = "teacher_"+date+".xlsx";
// 进行转码,使其支持中文文件名
response.reset();
response.setContentType("application/x-download; charset=utf-8");
//清除jsp页面缓存,用window open打开非jsp页面导出,有此项IE会报错
if (true) {
//http 1.1
response.setHeader("Cache-Control", "no-cache");
//http 1.0
response.setHeader("Pragma", "no-cache");
}//http 1.0和1.1都支持
response.setDateHeader("Expires", 0);
filename = URLEncoder.encode(filename, "UTF-8");
response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
workbook.write(response.getOutputStream());
}
需要一个对应的实体类
public class TeacherDto {
public String getUid() {
return uid;
} public void setUid(String uid) {
this.uid = uid;
} public String getTenant_id() {
return tenant_id;
} public void setTenant_id(String tenant_id) {
this.tenant_id = tenant_id;
} public String getSchool_id() {
return school_id;
} public void setSchool_id(String school_id) {
this.school_id = school_id;
} public String getNickname() {
return nickname;
} public void setNickname(String nickname) {
this.nickname = nickname;
} public Integer getSex() {
return sex;
} public void setSex(Integer sex) {
this.sex = sex;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getSchool() {
return school;
} public void setSchool(String school) {
this.school = school;
} public String getClazz() {
return clazz;
} public void setClazz(String clazz) {
this.clazz = clazz;
} public String getMobile() {
return mobile;
} public void setMobile(String mobile) {
this.mobile = mobile;
} public String getAccount() {
return account;
} public void setAccount(String account) {
this.account = account;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String toString() {
return "TeacherDto{" +
"uid='" + uid + '\'' +
", tenant_id='" + tenant_id + '\'' +
", school_id='" + school_id + '\'' +
", nickname='" + nickname + '\'' +
", sex=" + sex +
", age='" + age + '\'' +
", school='" + school + '\'' +
", clazz='" + clazz + '\'' +
", mobile='" + mobile + '\'' +
", account='" + account + '\'' +
", password='" + password + '\'' +
'}';
} private String uid;
@Excel(name="tenant_id")
private String tenant_id;
@Excel(name="school_id")
private String school_id;
@Excel(name="nickname")
private String nickname;
@Excel(name="sex")
private Integer sex;
@Excel(name="age")
private String age;
@Excel(name="school")
private String school;
@Excel(name="clazz")
private String clazz;
@Excel(name="mobile")
private String mobile;
@Excel(name="account")
private String account;
@Excel(name="password")
private String password;
}
前端使用的是layui
<form class="layui-form">
<div class="layui-inline">
<button class="layui-btn" id="importExcel" type="button"><i class="layui-icon"></i>导入</button>
</div>
<div class="layui-inline">
<button class="layui-btn" id="exportExcel" type="button"><i class="layui-icon"></i>导出</button>
</div>
</form>
<script> layui.use(['layer','form','table','upload'], function() {
var layer = layui.layer,
$ = layui.jquery,
form = layui.form,
table = layui.table,
upload = layui.upload;
//导入
upload.render({
elem: '#importExcel'
,url: "${base}/admin/excel/importTeacher"
,accept: 'file' //普通文件
,multiple: true
,done: function(res){
if(res.success){
layer.msg("上传成功",{time: 1000},function(){
//上传成功后刷新 });
}else{
if(res.message == "导入失败!" || res.message == "导入文件格式不对" || res.message == "导入文件没有数据"){
layer.msg(res.message);
}else{
layer.msg(res.message);
}
}
}
}); //导出
$('#exportExcel').on('click', function () {
window.open('${base}/admin/excel/exportTeacher');
// window.open('http://www.imooc.com/','_blank','width=400,height=500,menubar=no,toolbar=no');
}); });