只是后端代码,没有啥好看的,只能看swagger上的部分。。。
比如:
*我们还是直接上代码吧~~~~*
/***
* @Author Excel表格导入
* @data 2020 05/27 14:30
* @author wjb
*/
@RequestMapping(value = "/excel", method = RequestMethod.POST)
@ApiOperation(value = "用户表导入",notes = "导入demo")
public boolean upload(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException,InvalidFormatException {
Map<String, Object> resultMap = new HashMap<>();
List<SysAdminUser> list = new ArrayList<SysAdminUser>();
//1、得到上传的表
if (file.equals(null) || "".equals(file)){
System.out.println("file为空 = " + file);
return false;
}
Workbook workbook = WorkbookFactory.create(file.getInputStream());
//2、获取用户表
Sheet sheet = workbook.getSheet("Sheet1");
//3、获取表的总行数
int num = sheet.getLastRowNum();
System.out.println("总行数为 = " + num);
//4、总列数
int count = sheet.getRow(0).getLastCellNum();
System.out.println("总列数为 = " + count);
SysAdminUser sysAdminUser = new SysAdminUser();
//遍历每一行数据
for (int j = 1; j <= num; j++) {
Row row = sheet.getRow(j);
//如果单元格中有数字或者其他格式的数据,则调用setCellType()转换为string类
Cell account = row.getCell(0);
sysAdminUser.setAccount(account.getStringCellValue());
Cell accountName = row.getCell(1);
sysAdminUser.setAdminName(accountName.getStringCellValue());
Cell password = row.getCell(2);
sysAdminUser.setPassword(password.getStringCellValue());
Cell idCard = row.getCell(3);
sysAdminUser.setIdCard(idCard.getStringCellValue());
Cell phone = row.getCell(4);
sysAdminUser.setMobilePhone(phone.getStringCellValue());
Cell address = row.getCell(5);
sysAdminUser.setAddress(address.getStringCellValue());
list.add(sysAdminUser);
queryRepository.saveAll(list);
}
return true;
}
只用到一个controller层,让他直接跳到接口层去实现代码即可~~
有建议的可以留言,共勉!!!!