跳到主要内容

spu数据导入

需求

上传excel文件,里面是sku数据,每一行一个spu

上传完成后:在草稿箱中商品正常,上传图片后即可上架

模型

public class GoodsExcelData {

@ExcelProperty("编号")
private String sn;

@ExcelProperty("名称")
private String name;
}

导入任务

@Service
public class SpuImportTask {
public void importGoods(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();
return spuImportTask.importData(zipFile, admin.getPlatformShopId(), 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;
}