Commit 84248c81 authored by wangqiang's avatar wangqiang

添加:使用EasyExcel插件实现采购计划导入数据功能

parent b4158d7d
......@@ -86,6 +86,11 @@
<version>2.17.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.4</version>
</dependency>
</dependencies>
......
package com.yonde.dcs.plan.common.listener;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.yonde.dcs.plan.common.vo.ExtPuchasePlanExcelVO;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
@Slf4j
public class PurchasePlanExcelListenner extends AnalysisEventListener<ExtPuchasePlanExcelVO> {
/**
* 每隔100条处理下,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 100;
/**
* 缓存的数据
*/
private List<ExtPuchasePlanExcelVO> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
@Override
public void invoke(ExtPuchasePlanExcelVO extPuchasePlanExcelVO, AnalysisContext analysisContext) {
log.info("解析到一条数据:{}", extPuchasePlanExcelVO);
cachedDataList.add(extPuchasePlanExcelVO);
if (cachedDataList.size() >= BATCH_COUNT) {
// 处理缓存的数据,比如说入库。。。
// 然后清空
//todo 需要处理采购计划数据到数据库
cachedDataList.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// 收尾工作,处理剩下的缓存数据。。。
log.info("sheet={} 所有数据解析完成!", analysisContext.readSheetHolder().getSheetName());
}
/**
*
* @param exception
* @param context
* @throws Exception
*/
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
log.error("======>>>解析异常:", exception);
throw exception;
}
//todo wq测试代码
// public static void main(String[] args) {
// String fileName = "F:\\yangyi\\项目\\清华核研院\\核研院设计文档及原型\\采购计划导入模板.xlsx";
// EasyExcel.read(fileName, ExtPuchasePlanExcelVO.class, new PurchasePlanExcelListenner()).sheet().doRead();
// }
}
package com.yonde.dcs.plan.common.vo;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.CellType;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@EqualsAndHashCode(callSuper = false)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExtPuchasePlanExcelVO {
@ExcelProperty(value = {"采购计划","操作*"},index = 0)
@ColumnWidth(20)
private String operation;
/**
* 外协项目名称
*/
@ExcelProperty(value = {"采购计划","外协(外购)项目名称*"},index = 1)
@ColumnWidth(20)
private String outsourceProjectName;
/**
* 采购技术文件实际提交时间
*/
@ExcelProperty(value = {"采购计划","采购技术文件提交时间*"},index = 2)
@ColumnWidth(20)
private LocalDateTime purDesignFileSubmitTime;
/**
* 合同签订时间*
*/
@ExcelProperty(value = {"采购计划","合同签订时间*"},index = 3)
@ColumnWidth(20)
private LocalDateTime contractActualSigningTime;
/**
* 合同交付时间*
*/
@ExcelProperty(value = {"采购计划","合同交付时间*"},index = 4)
@ColumnWidth(20)
private String contractActualDeliveryTime;
/**
* 采购技术文件负责人
*/
@ExcelProperty(value = {"采购计划","采购技术文件负责人*"},index = 5)
@ColumnWidth(20)
private String purDesignFileManager;
/**
* 采购预算
*/
@ExcelProperty(value = {"采购计划","预算(万元)*"},index = 6)
@ColumnWidth(20)
private String purchaseBudget;
/**
* 项目代号
*/
@ExcelProperty(value = {"采购计划","项目代号*"},index = 7)
@ColumnWidth(20)
private String projectCode;
/**
* 密级
*/
@ExcelProperty(value = {"采购计划","密级*"},index = 8)
@ColumnWidth(20)
private String secretCode;
/**
* 备注
*/
@ExcelProperty(value = {"采购计划","备注*"},index = 9)
@ColumnWidth(20)
private String note;
public static void main(String[] args) {
List<ExtPuchasePlanExcelVO> list = new ArrayList<>();
for (int i=0;i<=1;i++){
ExtPuchasePlanExcelVO extPuchasePlanExcelVO = new ExtPuchasePlanExcelVO();
extPuchasePlanExcelVO.setNote("测试"+i);
list.add(extPuchasePlanExcelVO);
}
EasyExcel.write(
"F:\\yangyi\\项目\\清华核研院\\核研院设计文档及原型\\采购计划导入模板.xlsx", ExtPuchasePlanExcelVO.class)
.sheet("采购计划").doWrite(list);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment