|
|
|
@ -1,12 +1,7 @@
|
|
|
|
|
package com.alibaba.excel.write.executor; |
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Collection; |
|
|
|
|
import java.util.HashSet; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Set; |
|
|
|
|
import java.util.TreeMap; |
|
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
|
|
import com.alibaba.excel.context.WriteContext; |
|
|
|
|
import com.alibaba.excel.enums.HeadKindEnum; |
|
|
|
@ -22,12 +17,14 @@ import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
|
|
|
|
|
import com.alibaba.excel.write.metadata.CollectionRowData; |
|
|
|
|
import com.alibaba.excel.write.metadata.MapRowData; |
|
|
|
|
import com.alibaba.excel.write.metadata.RowData; |
|
|
|
|
import com.alibaba.excel.write.metadata.WriteTreeData; |
|
|
|
|
import com.alibaba.excel.write.metadata.holder.WriteHolder; |
|
|
|
|
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
|
|
|
|
|
|
|
|
|
import org.apache.commons.collections4.CollectionUtils; |
|
|
|
|
import org.apache.poi.ss.usermodel.Cell; |
|
|
|
|
import org.apache.poi.ss.usermodel.Row; |
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet; |
|
|
|
|
import org.springframework.cglib.beans.BeanMap; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -50,14 +47,37 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
|
|
|
|
|
if (writeSheetHolder.isNew() && !writeSheetHolder.getExcelWriteHeadProperty().hasHead()) { |
|
|
|
|
newRowIndex += writeContext.currentWriteHolder().relativeHeadRowIndex(); |
|
|
|
|
} |
|
|
|
|
// BeanMap is out of order, so use sortedAllFieldMap
|
|
|
|
|
Map<Integer, Field> sortedAllFieldMap = new TreeMap<>(); |
|
|
|
|
int relativeRowIndex = 0; |
|
|
|
|
for (Object oneRowData : data) { |
|
|
|
|
relativeRowIndex = loopDataByLevel(oneRowData, relativeRowIndex, newRowIndex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private int loopDataByLevel(Object oneRowData, int relativeRowIndex, int newRowIndex) { |
|
|
|
|
// BeanMap is out of order, so use sortedAllFieldMap
|
|
|
|
|
Map<Integer, Field> sortedAllFieldMap = new TreeMap<>(); |
|
|
|
|
int lastRowIndex = relativeRowIndex + newRowIndex; |
|
|
|
|
if (oneRowData instanceof WriteTreeData) { |
|
|
|
|
addOneRowOfDataToExcel(((WriteTreeData<?, ?>) oneRowData).getParentData(), lastRowIndex, relativeRowIndex, sortedAllFieldMap); |
|
|
|
|
relativeRowIndex++; |
|
|
|
|
List<?> childList = ((WriteTreeData<?, ?>) oneRowData).getChildDataList(); |
|
|
|
|
if (CollectionUtils.isNotEmpty(childList)) { |
|
|
|
|
int fromRow = relativeRowIndex; |
|
|
|
|
for (Object childData : childList) { |
|
|
|
|
relativeRowIndex = loopDataByLevel(childData, relativeRowIndex, newRowIndex); |
|
|
|
|
} |
|
|
|
|
setGroup(fromRow, relativeRowIndex); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
addOneRowOfDataToExcel(oneRowData, lastRowIndex, relativeRowIndex, sortedAllFieldMap); |
|
|
|
|
relativeRowIndex++; |
|
|
|
|
} |
|
|
|
|
return relativeRowIndex; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void setGroup(int fromRow, int toRow) { |
|
|
|
|
Sheet sheet = writeContext.writeSheetHolder().getSheet(); |
|
|
|
|
sheet.groupRow(fromRow + 1, toRow); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void addOneRowOfDataToExcel(Object oneRowData, int rowIndex, int relativeRowIndex, |
|
|
|
@ -156,7 +176,6 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
|
|
|
|
|
if (!beanKeySet.contains(name)) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, |
|
|
|
|
currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name); |
|
|
|
|
CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( |
|
|
|
|