From fba858f46abdb383f6659eae5a1cba7756c58cf9 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 13 Apr 2021 15:09:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A4=9A=E4=B8=AA=E8=A1=A8?= =?UTF-8?q?=E5=8D=95=E5=8F=AF=E8=83=BD=E6=A0=B7=E5=BC=8F=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../style/AbstractCellStyleStrategy.java | 38 ++++++++++++++-- .../AbstractVerticalCellStyleStrategy.java | 45 +++++++++++++------ 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java index 2e88cc84..f131c456 100644 --- a/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java @@ -35,9 +35,9 @@ public abstract class AbstractCellStyleStrategy implements CellWriteHandler, Wor return; } if (isHead) { - setHeadCellStyle(cell, head, relativeRowIndex); + setHeadCellStyle(writeSheetHolder, writeTableHolder, cell, head, relativeRowIndex); } else { - setContentCellStyle(cell, head, relativeRowIndex); + setContentCellStyle(writeSheetHolder, writeTableHolder, cell, head, relativeRowIndex); } } @@ -57,11 +57,27 @@ public abstract class AbstractCellStyleStrategy implements CellWriteHandler, Wor /** * Sets the cell style of header * + * @param writeSheetHolder + * @param writeTableHolder * @param cell * @param head * @param relativeRowIndex */ - protected abstract void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex); + protected void setHeadCellStyle(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, + Cell cell, Head head, Integer relativeRowIndex) { + setHeadCellStyle(cell, head, relativeRowIndex); + } + + /** + * Sets the cell style of header + * + * @param cell + * @param head + * @param relativeRowIndex + */ + protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) { + throw new UnsupportedOperationException("Custom styles must override the setHeadCellStyle method."); + } /** * Sets the cell style of content @@ -70,6 +86,20 @@ public abstract class AbstractCellStyleStrategy implements CellWriteHandler, Wor * @param head * @param relativeRowIndex */ - protected abstract void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex); + protected void setContentCellStyle(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, + Cell cell, Head head, Integer relativeRowIndex) { + setContentCellStyle(cell, head, relativeRowIndex); + } + + /** + * Sets the cell style of content + * + * @param cell + * @param head + * @param relativeRowIndex + */ + protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) { + throw new UnsupportedOperationException("Custom styles must override the setContentCellStyle method."); + } } diff --git a/src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java index 23292f6c..4ae5ac6a 100644 --- a/src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java @@ -1,10 +1,12 @@ package com.alibaba.excel.write.style; -import java.util.HashMap; import java.util.Map; import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.StyleUtil; +import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; +import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import org.apache.poi.ss.usermodel.Cell; @@ -19,8 +21,8 @@ import org.apache.poi.ss.usermodel.Workbook; public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyleStrategy { private Workbook workbook; - private Map headCellStyleCache = new HashMap(); - private Map contentCellStyleCache = new HashMap(); + private final Map>> headCellStyleCache = MapUtils.newHashMap(); + private final Map>> contentCellStyleCache = MapUtils.newHashMap(); @Override protected void initCellStyle(Workbook workbook) { @@ -28,13 +30,16 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl } @Override - protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) { + protected void setHeadCellStyle(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, + Head head, Integer relativeRowIndex) { if (head == null) { return; } + Map styleMap = getStyleMap(headCellStyleCache, writeSheetHolder, writeTableHolder); + int columnIndex = head.getColumnIndex(); - if (headCellStyleCache.containsKey(columnIndex)) { - CellStyle cellStyle = headCellStyleCache.get(columnIndex); + if (styleMap.containsKey(columnIndex)) { + CellStyle cellStyle = styleMap.get(columnIndex); if (cellStyle != null) { cell.setCellStyle(cellStyle); } @@ -42,22 +47,25 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl } WriteCellStyle headCellStyle = headCellStyle(head); if (headCellStyle == null) { - headCellStyleCache.put(columnIndex, StyleUtil.buildHeadCellStyle(workbook, null)); + styleMap.put(columnIndex, StyleUtil.buildHeadCellStyle(workbook, null)); } else { CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headCellStyle); - headCellStyleCache.put(columnIndex, cellStyle); + styleMap.put(columnIndex, cellStyle); cell.setCellStyle(cellStyle); } } @Override - protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) { + protected void setContentCellStyle(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, + Head head, Integer relativeRowIndex) { if (head == null) { return; } + Map styleMap = getStyleMap(contentCellStyleCache, writeSheetHolder, writeTableHolder); + int columnIndex = head.getColumnIndex(); - if (contentCellStyleCache.containsKey(columnIndex)) { - CellStyle cellStyle = contentCellStyleCache.get(columnIndex); + if (styleMap.containsKey(columnIndex)) { + CellStyle cellStyle = styleMap.get(columnIndex); if (cellStyle != null) { cell.setCellStyle(cellStyle); } @@ -65,10 +73,10 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl } WriteCellStyle contentCellStyle = contentCellStyle(cell, head, relativeRowIndex); if (contentCellStyle == null) { - contentCellStyleCache.put(columnIndex, StyleUtil.buildContentCellStyle(workbook, null)); + styleMap.put(columnIndex, StyleUtil.buildContentCellStyle(workbook, null)); } else { CellStyle cellStyle = StyleUtil.buildContentCellStyle(workbook, contentCellStyle); - contentCellStyleCache.put(columnIndex, cellStyle); + styleMap.put(columnIndex, cellStyle); cell.setCellStyle(cellStyle); } } @@ -105,4 +113,15 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl + "'contentCellStyle(Head head)' must be implemented."); } + private Map getStyleMap(Map>> cellStyleCache, + WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder) { + Map> tableStyleMap = cellStyleCache.computeIfAbsent( + writeSheetHolder.getSheetNo(), key -> MapUtils.newHashMap()); + Integer tableNo = 0; + if (writeTableHolder != null) { + tableNo = writeTableHolder.getTableNo(); + } + return tableStyleMap.computeIfAbsent(tableNo, key -> MapUtils.newHashMap()); + } + }