for (ReadSheet sheet : listSheet(stream, type)) { Integer sheetNo = sheet.getSheetNo(); String sheetName = sheet.getSheetName(); if(0 == sheetNo){ EasyExcel.read(stream,TbTradeBlotterOpSec.class, EasyExcelUtils.getReadListener(dataProcess())).excelType(type).charset(Charset.forName("UTF-8")).sheet(sheetNo).sheetName(sheetName).headRowNumber(1).doRead(); } if(1 == sheetNo){ EasyExcel.read(stream, TbTradeBlotterOpBondMaster.class, EasyExcelUtils.getReadListener(dataProcess2())).excelType(type).charset(Charset.forName("UTF-8")).sheet(sheetNo).sheetName(sheetName).headRowNumber(1).doRead(); } if(2==sheetNo){ EasyExcel.read(stream, TbTradeBlotterOpSecPos.class, EasyExcelUtils.getReadListener(dataProcess3())).excelType(type).charset(Charset.forName("UTF-8")).sheet(sheetNo).sheetName(sheetName).headRowNumber(1).doRead(); } if(3== sheetNo){ EasyExcel.read(stream,TbTradeBlotterOpMm.class, EasyExcelUtils.getReadListener(dataProcess4())).excelType(type).charset(Charset.forName("UTF-8")).sheet(sheetNo).sheetName(sheetName).headRowNumber(1).doRead(); } if(4==sheetNo){ EasyExcel.read(stream,TbTradeBlotterOpFx.class, EasyExcelUtils.getReadListener(dataProcess5())).excelType(type).charset(Charset.forName("UTF-8")).sheet(sheetNo).sheetName(sheetName).headRowNumber(1).doRead(); } if(5==sheetNo){ EasyExcel.read(stream,TbTradeBlotterOpCp.class, EasyExcelUtils.getReadListener(dataProcess6())).excelType(type).charset(Charset.forName("UTF-8")).sheet(sheetNo).sheetName(sheetName).headRowNumber(1).doRead(); } }简化这段代码
时间: 2024-02-21 12:58:19 浏览: 84
GetSheetName_execl_getsheetname_sheet_
可以将这段代码进行简化,使用一个 Map 对象来保存需要读取的表格类型和对应的 Class 类型,然后使用循环遍历 Map 对象,根据表格类型和 Class 类型来读取对应的表格。
简化后的代码如下所示:
```
Map<Integer, Class<?>> sheetMap = new HashMap<>();
sheetMap.put(0, TbTradeBlotterOpSec.class);
sheetMap.put(1, TbTradeBlotterOpBondMaster.class);
sheetMap.put(2, TbTradeBlotterOpSecPos.class);
sheetMap.put(3, TbTradeBlotterOpMm.class);
sheetMap.put(4, TbTradeBlotterOpFx.class);
sheetMap.put(5, TbTradeBlotterOpCp.class);
for (ReadSheet sheet : listSheet(stream, type)) {
Integer sheetNo = sheet.getSheetNo();
String sheetName = sheet.getSheetName();
Class<?> clazz = sheetMap.get(sheetNo);
if (clazz != null) {
EasyExcel.read(stream, clazz, EasyExcelUtils.getReadListener(dataProcess()))
.excelType(type)
.charset(Charset.forName("UTF-8"))
.sheet(sheetNo)
.sheetName(sheetName)
.headRowNumber(1)
.doRead();
}
}
```
这样做的好处是代码更加简洁,易于维护,如果需要增加新的表格类型,只需要在 Map 对象中添加对应的键值对即可。
阅读全文