From bc4ba98d58be35b31c62b348fc72612fb1f619ff Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 15 Sep 2021 20:37:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A1=AB=E5=85=85=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=A8=AA=E5=90=91=E6=A0=B7=E5=BC=8F=E7=AD=96=E7=95=A5?= =?UTF-8?q?=E6=8A=A5=E9=94=99=20[Issue=20#1651]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../style/HorizontalCellStyleStrategy.java | 34 ++++++++++++++----- update.md | 1 + 2 files changed, 27 insertions(+), 8 deletions(-) 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