sku数据导入
需求
上传excel文件,里面是sku数据,每一行一个spu,其中spu编号是必须的
模型
public class SkuExcelData {
@ExcelProperty("sku编号")
private String sku_sn;
@ExcelProperty("spu编号")
private String spu_sn;
@ExcelProperty("名称")
private String name;
}
导入任务
@Service
public class SkuImportTask {
public void importData(File file, Long sellerId, Long categoryId,String uuid) {
EasyExcel.read(file, GoodsExcelData.class, new PageReadListener<GoodsExcelData>(dataList -> {
for (GoodsExcelData demoData : dataList) {
System.out.println("读取到一条数据{}"+ JSON.toJSONString(demoData) +" "+ new Date());
}
})).sheet().doRead();
}
}
controller
public boolean batchImports(MultipartFile file, @ApiIgnore Long categoryId, @ApiIgnore Long shopCatId){
File excelFile = convertFile(file);
Admin admin = AdminUserContext.getAdmin();
spuImportTask.importData(zipFile, admin.getPlatformShopId(), categoryId, shopCatId);
return goodsImportTask.goodsImport(zipFile, seller.getSellerId(), categoryId, shopCatId);
}
private File convertFile(MultipartFile mpFile) throws IOException {
// 创建临时文件
File file = File.createTempFile("import", ".tmp");
// 将上传的文件流copy到刚创建的临时文件中
InputStream inputStream = mpFile.getInputStream();
FileUtils.copyInputStreamToFile(inputStream, file);
return file;
}