use of com.alibaba.excel.metadata.BaseRowModel in project SpringBoot-Hello by ruiyeclub.
the class ExcelUtil method writeExcel.
/**
* @param outputStream Excel的输出流
* @param sheetAndTable sheet和table名,格式:<sheet名,<table名集合>>
* @param data <sheet名,<table名,table数据集>>
* @param clazz <sheet名,<table名,table数据集实体class类型>>
* @param excelTypeEnum Excel的格式(XLS或XLSX)
* @Description: 使用模型来写入Excel,多sheet,多table
* @Date: 2020/1/16 21:43
* @Return: byte[]
* @Throws: Exception
*/
public static byte[] writeExcel(ByteArrayOutputStream outputStream, Map<String, List<String>> sheetAndTable, Map<String, Map<String, List<? extends BaseRowModel>>> data, Map<String, Map<String, Class<? extends BaseRowModel>>> clazz, ExcelTypeEnum excelTypeEnum) throws Exception {
// 这里指定需要表头,因为model通常包含表头信息
ExcelWriter writer = new ExcelWriter(outputStream, excelTypeEnum, true);
Iterator<Map.Entry<String, List<String>>> iterator = sheetAndTable.entrySet().iterator();
int sheetNo = 1;
// 遍历sheet
while (iterator.hasNext()) {
Map.Entry<String, List<String>> next = iterator.next();
// 当前sheet名
String sheetName = next.getKey();
// 当前sheet对应的table的实体类class对象集合
Map<String, Class<? extends BaseRowModel>> tableClasses = clazz.get(sheetName);
// 当前sheet对应的table的数据集合
Map<String, List<? extends BaseRowModel>> dataListMaps = data.get(sheetName);
Sheet sheet = new Sheet(sheetNo, 0);
sheet.setSheetName(sheetName);
int tableNo = 1;
Iterator<Map.Entry<String, Class<? extends BaseRowModel>>> iterator1 = tableClasses.entrySet().iterator();
// 遍历table
while (iterator1.hasNext()) {
Map.Entry<String, Class<? extends BaseRowModel>> next1 = iterator1.next();
// 当前table名
String tableName = next1.getKey();
// 当前table对应的class
Class<? extends BaseRowModel> tableClass = next1.getValue();
// 当前table对应的数据集
List<? extends BaseRowModel> tableData = dataListMaps.get(tableName);
Table table = new Table(tableNo);
table.setClazz(tableClass);
writer.write(tableData, sheet, table);
tableNo++;
}
sheetNo++;
}
writer.finish();
return outputStream.toByteArray();
}
Aggregations