//title监听器
@Slf4j
public class TitleListener extends AnalysisEventListener<Title> {
private final List<Title> rows = new ArrayList<>();
@Override
public void invoke(Title title, AnalysisContext analysisContext) {
rows.add(title);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
public List<Title> getRows() {
return rows;
}
}
===============================================================
//SignalListener 监听器
@Slf4j
public class SignalListener extends AnalysisEventListener<Signal> {
private final List<Signal> rows = new ArrayList<>();
private static String subdivisionalWork = "";
private static String partitionedPrj = "";
private static String singlePrj="";
@Override
public void invoke(Signal signal, AnalysisContext analysisContext) {
if (null != signal.getSubdivisionalWork()) {
subdivisionalWork = signal.getSubdivisionalWork();
}
if (null != signal.getPartitionedPrj()) {
partitionedPrj = signal.getPartitionedPrj();
}
if (null != signal.getSinglePrj()) {
singlePrj = signal.getSinglePrj();
}
signal.setSubdivisionalWork(subdivisionalWork);
signal.setPartitionedPrj(partitionedPrj);
signal.setSinglePrj(singlePrj);
rows.add(signal);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
public List<Signal> getRows() {
return rows;
}
}
=============================================================================
//实体
@Data
public class Signal extends BaseRowModel {
@ExcelProperty(value = {"单项", "单项"}, index = 0)
private String singlePrj="";
@ExcelProperty(value = {"分部", "分部"}, index = 1)
private String partitionedPrj="";
@ExcelProperty(value = {"分项", "分项"}, index = 2)
private String subdivisionalWork="";
@ExcelProperty(value = {"工作事项清单", "工作事项清单"}, index = 3)
private String workItem = "";
@ExcelProperty(value = {"单位", "单位"}, index = 4)
private String description="";
@ExcelProperty(value = {"设计总数量", "左"}, index = 5)
private String designLeft ="0";
@ExcelProperty(value = {"设计总数量", "右"}, index = 6)
private String designRight = "0";
@ExcelProperty(value = {"设计总数量", "设计总数量"}, index = 7)
private String designTotal = "0";
@ExcelProperty(value = {"当日完成数量", "左"}, index = 8)
private String currentLeft = "0";
@ExcelProperty(value = {"当日完成数量", "右"}, index = 9)
private String currentRight = "0";
@ExcelProperty(value = {"当日完成数量", "当日完成数量"}, index = 10)
private String currentTotal = "0";
@ExcelProperty(value = "备注", index = 11)
private String comment;
}
//实体
@Data
public class Title extends BaseRowModel {
@ExcelProperty(value = "项目名称",index = 1)
private String proName;
}
=======================================================================================================
部分代码
@ResponseBody
public Result<String> read4File(MultipartFile[] file) throws Exception {
for (int i = 0; i < file.length; i++) {
//循环的部分
ExcelReader excelReaderOnePart = EasyExcel.read(file[i].getInputStream()).build();
ExcelReader excelReaderTwoPart = EasyExcel.read(file[i].getInputStream()).build();
List<ReadSheet> readSheets = EasyExcel.read(file[i].getInputStream()).build().excelExecutor().sheetList();
if (!readSheets.isEmpty()) {
for (ReadSheet readSheet : readSheets) {
//ExcelListener<Title> listener = new ExcelListener<>();
TitleListener titleListener=new TitleListener();
SignalListener signalListener = new SignalListener();
//第一部分从第一行开始读
readSheet = EasyExcel.readSheet(readSheet.getSheetNo()).head(Title.class).registerReadListener(titleListener).headRowNumber(1).build();
excelReaderOnePart.read(readSheet);
//第二部分从第7行开始读
readSheet = EasyExcel.readSheet(readSheet.getSheetNo()).head(Signal.class).registerReadListener(signalListener).headRowNumber(7).build();
excelReaderTwoPart.read(readSheet);
//读取名称
String proName = titleListener.getRows().get(0).getProName();
//读取单位名称
String unitName = titleListener.getRows().get(1).getProName();
//读取日期
String excelDate = titleListener.getRows().get(2).getProName();
//拿到读取到的集合
List<Signal> signals = signalListener.getRows();
// doSomething
.......
}
excelReaderOnePart.finish();
excelReaderTwoPart.finish();
}
}