1、自定义模版如下:
| 销量校验异常数据 | ||||||
| 序号 | spu编码 | sku编码 | 商品名称 | 销量 | ||
| mysql | redis | mongodb | ||||
| {.rowIndex} | {.productCode} | {.skuCode} | {.productName} | {.salesVolume} | {.redisVolume} | {.mongoVolume} |
2、源码部分:
目录结构:

A、引入EasyExcel
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
B、创建excel模版填充对应java bean
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* @author shipj
* @version 1.0
* @Title: ExcelSellPricePagerDTO
* @Description: 巡检销量
* @date 2021-06-17 15:07
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GoodsSalesExcelDTO implements Serializable {
/**
*序号
*/
private Integer rowIndex;
/**
*SPU编码
*/
private String productCode;
/**
* SKU编码
*/
private String skuCode;
/**
*商品名称
*/
private String productName;
/**
*销量
*/
private Integer salesVolume;
/**
*redis缓存销量
*/
private Integer redisVolume;
/**
*mongo数量
*/
private Integer mongoVolume;
}
C、创建模版数据填充方法
public Map<String, Object> getExcelFileInfo(List<GoodsSalesExcelDTO> listExcel) {
Map<String,Object> excelInfo = new HashMap<>();
try {
//获取模版文件资源信息、流信息
String templateFileName = "GoodsSalesCheckData.xlsx";
ClassPathResource classPathResource = new ClassPathResource("template"+File.separator+"excel"+File.separator+templateFileName);
InputStream inputStream = classPathResource.getInputStream();
//临时文件目录
ClassPathResource tempResource = new ClassPathResource("template"+File.separator+"excel"+File.separator+"temp");
String tempPath = tempResource.getFile().getAbsolutePath();
//临时文件名称
String tempFileName = IdWorker.getMillisecond().toString()+"_GoodsSalesCheckData.xlsx";
//临时文件路径及名称
String tempFinalPath = null;
tempFinalPath = tempPath+ File.separator+tempFileName;
log.info("临时文件路径及名称 {}",tempFinalPath);
// 写入excel
ExcelWriter excelWriter = EasyExcel.write(tempFinalPath).withTemplate(inputStream).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
// 填充集合 {.name}
excelWriter.fill(listExcel, writeSheet);
excelWriter.finish();
//构建返回map
excelInfo.put("templateFileName",templateFileName);
excelInfo.put("tempFileName",tempFileName);
excelInfo.put("tempFinalPath",tempFinalPath);
excelInfo.put("dataSize",listExcel.size());
excelInfo.put("isDelete",true);
} catch (Exception e) {
e.printStackTrace();
log.info("读取文件异常:{}", e.getMessage());
}
return excelInfo;
}
D、调用测试
@Test
public void getExcelFileInfo(){
List<GoodsSalesExcelDTO> listExcel = new ArrayList<>();
GoodsSalesExcelDTO dto1 = new GoodsSalesExcelDTO();
dto1.setRowIndex(1);
dto1.setProductCode("PC1");
dto1.setSkuCode("SC1");
dto1.setProductName("测试1");
dto1.setSalesVolume(100);
dto1.setRedisVolume(100);
dto1.setMongoVolume(100);
GoodsSalesExcelDTO dto2 = new GoodsSalesExcelDTO();
dto2.setRowIndex(2);
dto2.setProductCode("PC2");
dto2.setSkuCode("SC2");
dto2.setProductName("测试2");
dto2.setSalesVolume(20);
dto2.setRedisVolume(10);
dto2.setMongoVolume(10);
listExcel.add(dto1);
listExcel.add(dto2);
goodsSalesCheckService.getExcelFileInfo(listExcel);
}
根据excel模版填充数据之后,excel效果如下:
| 销量校验异常数据 | ||||||
| 序号 | spu编码 | sku编码 | 商品名称 | 销量 | ||
| mysql | redis | mongodb | ||||
| 1 | PC1 | SC1 | 测试1 | 100 | 100 | 100 |
| 2 | PC2 | SC2 | 测试2 | 20 | 10 | 10 |
2536




被折叠的 条评论
为什么被折叠?



