1、Controller
//导入excel
@RequestMapping(value = "/import", method=RequestMethod.POST)
public JsonDto importExcel(@RequestParam(value="file",required = false) MultipartFile file,
HttpServletRequest request,
HttpServletResponse response){
return functionProjectRelationService.importExcelFile(file);
}
2、Service
public JsonDto importExcelFile(MultipartFile file) {
JsonDto jsonDto = new JsonDto();
try{
int successNum = 0;
int failureNum = 0;
ImportExcel ei = new ImportExcel(file, 0, 0);
List<User> list = ei.getDataList(User.class);
logger.info("导入" + list.size() + "条数据");
jsonDto.setSuccess(Boolean.TRUE);
jsonDto.setErrcode("200");
jsonDto.setErrmsg("上传成功");
jsonDto.setData(list);
}catch(Exception e){
e.printStackTrace();
jsonDto.setSuccess(Boolean.TRUE);
jsonDto.setErrcode("-1");
jsonDto.setErrmsg("上传异常"+e.getMessage());
jsonDto.setData(new JSONObject());
return jsonDto;
}
return jsonDto;
}
3、User.java
package com.elink.estos.common.utils.excel;
import com.elink.estos.common.utils.excel.annotation.ExcelField;
/**
* @Auther: yinzuomei
* @Date: 2019/9/3 13:51
* @Description:
*/
public class User {
private String name;
private String sex;
private String age;
private String address;
@ExcelField(title="姓名", align=1,sort = 1)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ExcelField(title="性别", align=1,sort = 2)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@ExcelField(title="年龄", align=1,sort = 3)
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@ExcelField(title="地址", align=1,sort = 4)
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
4、工具类修改