1. 导入依赖
<!-- POI相关依赖 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
2. 实体类
package com.ssm.bean;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class Users {
private Long uid;
private String uno;
private String uname;
private String password;
private String sex;
private Integer age;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday;
private String phone;
private String address;
private Long type;
private String uhead;
public Users() {
}
//实现Constructor、getter、setter、toString()
}
3. 业务层使用POI组件导入
//导入用户
@RequestMapping("/improt")
public String ImportUSer(@RequestParam("importFile") MultipartFile importFile, HttpServletRequest request){
System.out.println("导入操作!");
//使用MD5加密
System.out.println("文件是否为空:" + importFile.isEmpty());
System.out.println("文件原名:" + importFile.getOriginalFilename());
//使用UUID,以免文件名重复
UUID uuid = UUID.randomUUID();
//新的文件名
String newFileName = uuid.toString() + ".xlsx";
System.out.println("新的文件名:" + newFileName);
//获取application对象