|
|
@ -1,10 +1,12 @@ |
|
|
|
package com.alibaba.excel.write.style; |
|
|
|
package com.alibaba.excel.write.style; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.excel.metadata.Head; |
|
|
|
import com.alibaba.excel.metadata.Head; |
|
|
|
|
|
|
|
import com.alibaba.excel.util.MapUtils; |
|
|
|
import com.alibaba.excel.util.StyleUtil; |
|
|
|
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 com.alibaba.excel.write.metadata.style.WriteCellStyle; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.Cell; |
|
|
|
import org.apache.poi.ss.usermodel.Cell; |
|
|
@ -19,8 +21,8 @@ import org.apache.poi.ss.usermodel.Workbook; |
|
|
|
public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyleStrategy { |
|
|
|
public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyleStrategy { |
|
|
|
|
|
|
|
|
|
|
|
private Workbook workbook; |
|
|
|
private Workbook workbook; |
|
|
|
private Map<Integer, CellStyle> headCellStyleCache = new HashMap<Integer, CellStyle>(); |
|
|
|
private final Map<Integer, Map<Integer, Map<Integer, CellStyle>>> headCellStyleCache = MapUtils.newHashMap(); |
|
|
|
private Map<Integer, CellStyle> contentCellStyleCache = new HashMap<Integer, CellStyle>(); |
|
|
|
private final Map<Integer, Map<Integer, Map<Integer, CellStyle>>> contentCellStyleCache = MapUtils.newHashMap(); |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void initCellStyle(Workbook workbook) { |
|
|
|
protected void initCellStyle(Workbook workbook) { |
|
|
@ -28,13 +30,16 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@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) { |
|
|
|
if (head == null) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Map<Integer, CellStyle> styleMap = getStyleMap(headCellStyleCache, writeSheetHolder, writeTableHolder); |
|
|
|
|
|
|
|
|
|
|
|
int columnIndex = head.getColumnIndex(); |
|
|
|
int columnIndex = head.getColumnIndex(); |
|
|
|
if (headCellStyleCache.containsKey(columnIndex)) { |
|
|
|
if (styleMap.containsKey(columnIndex)) { |
|
|
|
CellStyle cellStyle = headCellStyleCache.get(columnIndex); |
|
|
|
CellStyle cellStyle = styleMap.get(columnIndex); |
|
|
|
if (cellStyle != null) { |
|
|
|
if (cellStyle != null) { |
|
|
|
cell.setCellStyle(cellStyle); |
|
|
|
cell.setCellStyle(cellStyle); |
|
|
|
} |
|
|
|
} |
|
|
@ -42,22 +47,25 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl |
|
|
|
} |
|
|
|
} |
|
|
|
WriteCellStyle headCellStyle = headCellStyle(head); |
|
|
|
WriteCellStyle headCellStyle = headCellStyle(head); |
|
|
|
if (headCellStyle == null) { |
|
|
|
if (headCellStyle == null) { |
|
|
|
headCellStyleCache.put(columnIndex, StyleUtil.buildHeadCellStyle(workbook, null)); |
|
|
|
styleMap.put(columnIndex, StyleUtil.buildHeadCellStyle(workbook, null)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headCellStyle); |
|
|
|
CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headCellStyle); |
|
|
|
headCellStyleCache.put(columnIndex, cellStyle); |
|
|
|
styleMap.put(columnIndex, cellStyle); |
|
|
|
cell.setCellStyle(cellStyle); |
|
|
|
cell.setCellStyle(cellStyle); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@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) { |
|
|
|
if (head == null) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Map<Integer, CellStyle> styleMap = getStyleMap(contentCellStyleCache, writeSheetHolder, writeTableHolder); |
|
|
|
|
|
|
|
|
|
|
|
int columnIndex = head.getColumnIndex(); |
|
|
|
int columnIndex = head.getColumnIndex(); |
|
|
|
if (contentCellStyleCache.containsKey(columnIndex)) { |
|
|
|
if (styleMap.containsKey(columnIndex)) { |
|
|
|
CellStyle cellStyle = contentCellStyleCache.get(columnIndex); |
|
|
|
CellStyle cellStyle = styleMap.get(columnIndex); |
|
|
|
if (cellStyle != null) { |
|
|
|
if (cellStyle != null) { |
|
|
|
cell.setCellStyle(cellStyle); |
|
|
|
cell.setCellStyle(cellStyle); |
|
|
|
} |
|
|
|
} |
|
|
@ -65,10 +73,10 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl |
|
|
|
} |
|
|
|
} |
|
|
|
WriteCellStyle contentCellStyle = contentCellStyle(cell, head, relativeRowIndex); |
|
|
|
WriteCellStyle contentCellStyle = contentCellStyle(cell, head, relativeRowIndex); |
|
|
|
if (contentCellStyle == null) { |
|
|
|
if (contentCellStyle == null) { |
|
|
|
contentCellStyleCache.put(columnIndex, StyleUtil.buildContentCellStyle(workbook, null)); |
|
|
|
styleMap.put(columnIndex, StyleUtil.buildContentCellStyle(workbook, null)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
CellStyle cellStyle = StyleUtil.buildContentCellStyle(workbook, contentCellStyle); |
|
|
|
CellStyle cellStyle = StyleUtil.buildContentCellStyle(workbook, contentCellStyle); |
|
|
|
contentCellStyleCache.put(columnIndex, cellStyle); |
|
|
|
styleMap.put(columnIndex, cellStyle); |
|
|
|
cell.setCellStyle(cellStyle); |
|
|
|
cell.setCellStyle(cellStyle); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -105,4 +113,15 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl |
|
|
|
+ "'contentCellStyle(Head head)' must be implemented."); |
|
|
|
+ "'contentCellStyle(Head head)' must be implemented."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Map<Integer, CellStyle> getStyleMap(Map<Integer, Map<Integer, Map<Integer, CellStyle>>> cellStyleCache, |
|
|
|
|
|
|
|
WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder) { |
|
|
|
|
|
|
|
Map<Integer, Map<Integer, CellStyle>> tableStyleMap = cellStyleCache.computeIfAbsent( |
|
|
|
|
|
|
|
writeSheetHolder.getSheetNo(), key -> MapUtils.newHashMap()); |
|
|
|
|
|
|
|
Integer tableNo = 0; |
|
|
|
|
|
|
|
if (writeTableHolder != null) { |
|
|
|
|
|
|
|
tableNo = writeTableHolder.getTableNo(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return tableStyleMap.computeIfAbsent(tableNo, key -> MapUtils.newHashMap()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|