😊 @ 作者: 瓶盖子io
💖 @ 主页: 瓶盖子io-CSDN博客
//前端upload上传组件
<el-upload
action="/brand/batchBrand"
:on-success="handleAvatarSuccess"
>
<el-button>导入</el-button>
</el-upload>
action,跳转到后端地址,在这个请求路径下执行导入逻辑
:on-success对应的method
handleAvatarSuccess(res) { this.$message.success(res); //this.pageSize=20; //this.selectAll(); },
//注释的可写可不写
//导入
@PostMapping("/batchBrand")
public String batchBrand(MultipartFile file) throws IOException{
//创建工作薄对象(读用户上传上来的文件)
Workbook workbook=new HSSFWorkbook(file.getInputStream());
//定义一个集合用来存储数据
List<Brand> list=new ArrayList<>();
//获取表对象
Sheet sheet = workbook.getSheetAt(0);
//获取表中最后一行的行号
int lastRowNum = sheet.getLastRowNum();
//把表里每一行数据都拿到
for (int i = 1; i <= lastRowNum; i++) {
//通过表对象调用方法获取行对象
Row row = sheet.getRow(i);
//获取行上的每个单元格中的文本值
int id = (int) row.getCell(0).getNumericCellValue();
String bname = row.getCell(1).getStringCellValue();
String company = row.getCell(2).getStringCellValue();
int stort = (int) row.getCell(3).getNumericCellValue();
int status = (int) row.getCell(4).getNumericCellValue();
int aid = (int) row.getCell(5).getNumericCellValue();
//将数据赋值给brand
Brand brand=new Brand(id,bname,company,stort,status,aid);
//将赋值后的学生对象添加到list中
list.add(brand);
}
//关闭资源
workbook.close();
//把数据放到表里
brandService.saveBatch(list);
return "批量添加成功,OK";
}