package com.taover.easyexcel.write.style; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Workbook; import com.taover.easyexcel.metadata.Head; import com.taover.easyexcel.util.StyleUtil; import com.taover.easyexcel.write.metadata.style.WriteCellStyle; /** * Use the same style for the column * * @author Jiaju Zhuang */ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyleStrategy { private Workbook workbook; private Map headCellStyleCache = new HashMap(); private Map contentCellStyleCache = new HashMap(); @Override protected void initCellStyle(Workbook workbook) { this.workbook = workbook; } @Override protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) { if (head == null) { return; } int columnIndex = head.getColumnIndex(); if (headCellStyleCache.containsKey(columnIndex)) { CellStyle cellStyle = headCellStyleCache.get(columnIndex); if (cellStyle != null) { cell.setCellStyle(cellStyle); } return; } WriteCellStyle headCellStyle = headCellStyle(head); if (headCellStyle == null) { headCellStyleCache.put(columnIndex, null); } else { CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headCellStyle); headCellStyleCache.put(columnIndex, cellStyle); cell.setCellStyle(cellStyle); } } @Override protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) { if (head == null) { return; } int columnIndex = head.getColumnIndex(); if (contentCellStyleCache.containsKey(columnIndex)) { CellStyle cellStyle = contentCellStyleCache.get(columnIndex); if (cellStyle != null) { cell.setCellStyle(cellStyle); } return; } WriteCellStyle contentCellStyle = contentCellStyle(head); if (contentCellStyle == null) { contentCellStyleCache.put(columnIndex, null); } else { CellStyle cellStyle = StyleUtil.buildContentCellStyle(workbook, contentCellStyle); contentCellStyleCache.put(columnIndex, cellStyle); cell.setCellStyle(cellStyle); } } /** * Returns the column width corresponding to each column head * * @param head Nullable * @return */ protected abstract WriteCellStyle headCellStyle(Head head); /** * Returns the column width corresponding to each column head * * @param head Nullable * @return */ protected abstract WriteCellStyle contentCellStyle(Head head); }