diff --git a/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java index 0d4228f5..d33a2a74 100644 --- a/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java @@ -2,7 +2,7 @@ package com.alibaba.excel.write.style; import java.util.List; -import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.metadata.style.WriteCellStyle; @@ -14,7 +14,7 @@ import org.apache.commons.collections4.CollectionUtils; * * @author Jiaju Zhuang */ -public class HorizontalCellStyleStrategy extends AbstractVerticalCellStyleStrategy { +public class HorizontalCellStyleStrategy extends AbstractCellStyleStrategy { private WriteCellStyle headWriteCellStyle; private List contentWriteCellStyleList; @@ -33,16 +33,34 @@ public class HorizontalCellStyleStrategy extends AbstractVerticalCellStyleStrate } @Override - protected WriteCellStyle headCellStyle(Head head) { - return headWriteCellStyle; + protected void setHeadCellStyle(CellWriteHandlerContext context) { + if (!continueProcessing(context) || headWriteCellStyle == null) { + return; + } + WriteCellData cellData = context.getCellDataList().get(0); + cellData.setWriteCellStyle(headWriteCellStyle); } @Override - protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) { - if (CollectionUtils.isEmpty(contentWriteCellStyleList)) { - return null; + protected void setContentCellStyle(CellWriteHandlerContext context) { + if (!continueProcessing(context) || CollectionUtils.isEmpty(contentWriteCellStyleList)) { + return; + } + WriteCellData cellData = context.getCellDataList().get(0); + if (context.getRelativeRowIndex() == null || context.getRelativeRowIndex() <= 0) { + cellData.setWriteCellStyle(contentWriteCellStyleList.get(0)); + } else { + cellData.setWriteCellStyle( + contentWriteCellStyleList.get(context.getRelativeRowIndex() % contentWriteCellStyleList.size())); + } + } + + protected boolean continueProcessing(CellWriteHandlerContext context) { + List> cellDataList = context.getCellDataList(); + if (CollectionUtils.isEmpty(cellDataList) || cellDataList.size() > 1) { + return false; } - return contentWriteCellStyleList.get(context.getRelativeRowIndex() % contentWriteCellStyleList.size()); + return true; } } diff --git a/update.md b/update.md index 4d4d96dc..c2cbf89c 100644 --- a/update.md +++ b/update.md @@ -19,6 +19,7 @@ * 修改填充可能填充错误的bug [Issue #2035](https://github.com/alibaba/easyexcel/issues/2035) * 修复无对象读 返回map的size可能会头的size不一致 [Issue #2014](https://github.com/alibaba/easyexcel/issues/2014) * 修复合并头可能异常的bug [Issue #1662](https://github.com/alibaba/easyexcel/issues/1662) +* 修复填充调用横向样式策略报错 [Issue #1651](https://github.com/alibaba/easyexcel/issues/1651) # 2.2.10