1. 导入依赖
org.apache.poi
poi
3.17
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对象
ServletContext application = request.getServletContext();
//获取服务器真实路径
String realPath = application.getRealPath("/files/imp");
System.out.println("服务器真实路径:" + realPath);
File newFolder = creatNewFolder(realPath);
//内存中的文件
File file = new File(newFolder, newFileName);
//获取文件的绝对路径
String absolutePath = file.getAbsolutePath();
System.out.println("绝对路径:" + absolutePath);
try {
//上传到绝对路径,写到硬盘
importFile.transferTo(new File(absolutePath));
} catch (IOException e) {
System.out.println("IOException");
}
System.out.println("开始导入数据-----------------");
try {
//读文件到内存
FileInputStream fis = new FileInputStream(new File(absolutePath));
//获取工作簿对象
HSSFWorkbook workbook = new HSSFWorkbook(fis);
//获取第一个sheet
HSSFSheet sheet = workbook.getSheetAt(0);
//遍历每一行(除去第一行)
List users = new ArrayList<>();
for(int i = 1; i <= sheet.getLastRowNum(); i++){
HSSFRow row = sheet.getRow(i);
if(row==null){
continue;
}
Users u = new Users();
//遍历每一行的每个单元格
for(int j = 0; j < row.getLastCellNum(); j++){
//每个单元格
HSSFCell cell = row.getCell(j);
//单元格的类型
CellType cellTypeEnum = cell.getCellTypeEnum();
Object cellValue = null;
System.out.println("类型:" + j + ":" + cellTypeEnum);
//String类型
if(CellType.STRING.equals(cellTypeEnum)){
cellValue = cell.getStringCellValue();
}
//数字类型
if(CellType.NUMERIC.equals(cellTypeEnum)){
cellValue = cell.getNumericCellValue();
}
if(cellValue == null){
cellValue = "";
}
//将单元格中的值装到对象中
switch (j){
case 0:
u.setUno((String)cellValue);
System.out.println("++++++++0:" + cellValue);
break;
case 1:
u.setUname((String)cellValue);
System.out.println("++++++++1:" + cellValue);
break;
case 2:
String password = MD5Util.MD5Encode((String) cellValue, "UTF-8");
u.setPassword(password);
System.out.println("++++++++2:" + cellValue);
break;
case 3:
u.setSex((String)cellValue);
System.out.println("++++++++3:" + cellValue);
break;
case 4:
u.setAge( Integer.parseInt(new java.text.DecimalFormat("0").format(cellValue)));
System.out.println("++++++++4:" + cellValue);
break;
case 5:
u.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse((String)cellValue));
System.out.println("++++++++5:" + cellValue);
break;
case 6:
//数据过大,转成String会变成科学记数法
String phone = new BigDecimal((Double) cellValue).toString();
u.setPhone(phone);
System.out.println("++++++++6:" + phone);
break;
case 7:
u.setAddress((String)cellValue);
System.out.println("++++++++7:" + cellValue);
break;
case 8:
switch ((String)cellValue){
case "管理员":
u.setType(1L);
case "经理":
u.setType(2L);
case "普通用户":
u.setType(3L);
}
System.out.println("++++++++8:" + cellValue);
break;
case 9:
u.setUhead((String)cellValue);
System.out.println("++++++++9:" + cellValue);
break;
default:
break;
}
}
users.add(u);
}
System.out.println("导入完毕-----------------");
for (Users user : users) {
System.out.println(user);
//增加到数据库
userService.addUser(user);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException");
} catch (ParseException e) {
System.out.println("ParseException");
}
System.out.println("导入操作完成!");
//转到首页
return "redirect:list";
}
4. 测试数据截图
5. 测试数据见附件
https://download.csdn.net/download/dmxexcalibur/11197487