Browse Source

修改多个表单可能样式重复的问题

developing
Jiaju Zhuang 4 years ago
parent
commit
fba858f46a
  1. 38
      src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java
  2. 45
      src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java

38
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);
}
}
@ -54,6 +54,20 @@ public abstract class AbstractCellStyleStrategy implements CellWriteHandler, Wor
*/
protected abstract void initCellStyle(Workbook workbook);
/**
* Sets the cell style of header
*
* @param writeSheetHolder
* @param writeTableHolder
* @param cell
* @param head
* @param relativeRowIndex
*/
protected void setHeadCellStyle(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
Cell cell, Head head, Integer relativeRowIndex) {
setHeadCellStyle(cell, head, relativeRowIndex);
}
/**
* Sets the cell style of header
*
@ -61,7 +75,21 @@ public abstract class AbstractCellStyleStrategy implements CellWriteHandler, Wor
* @param head
* @param relativeRowIndex
*/
protected abstract void setHeadCellStyle(Cell cell, Head head, Integer 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
*
* @param cell
* @param head
* @param relativeRowIndex
*/
protected void setContentCellStyle(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
Cell cell, Head head, Integer relativeRowIndex) {
setContentCellStyle(cell, head, relativeRowIndex);
}
/**
* Sets the cell style of content
@ -70,6 +98,8 @@ public abstract class AbstractCellStyleStrategy implements CellWriteHandler, Wor
* @param head
* @param relativeRowIndex
*/
protected abstract void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex);
protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
throw new UnsupportedOperationException("Custom styles must override the setContentCellStyle method.");
}
}

45
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<Integer, CellStyle> headCellStyleCache = new HashMap<Integer, CellStyle>();
private Map<Integer, CellStyle> contentCellStyleCache = new HashMap<Integer, CellStyle>();
private final Map<Integer, Map<Integer, Map<Integer, CellStyle>>> headCellStyleCache = MapUtils.newHashMap();
private final Map<Integer, Map<Integer, Map<Integer, CellStyle>>> 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<Integer, CellStyle> 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<Integer, CellStyle> 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<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());
}
}

Loading…
Cancel
Save