Browse Source

输出时合并单元格增加行合并选项

developing
zl 5 years ago
parent
commit
6ecdb90abd
  1. 19
      src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java

19
src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java

@ -13,24 +13,39 @@ import com.alibaba.excel.metadata.Head;
*/ */
public class LoopMergeStrategy extends AbstractMergeStrategy { public class LoopMergeStrategy extends AbstractMergeStrategy {
private int eachRow; private int eachRow;
private int columnCount;
private int columnIndex; private int columnIndex;
public LoopMergeStrategy(int eachRow, int columnIndex) { public LoopMergeStrategy(int eachRow, int columnIndex) {
this(eachRow, 1, columnIndex);
}
public LoopMergeStrategy(int eachRow, int columnCount, int columnIndex) {
if (eachRow < 1) { if (eachRow < 1) {
throw new IllegalArgumentException("EachRows must be greater than 1"); throw new IllegalArgumentException("EachRows must be greater than 1");
} }
if (columnCount < 1) {
throw new IllegalArgumentException("ColumnCount must be greater than 1");
}
if (columnCount == 1 && eachRow == 1) {
throw new IllegalArgumentException("ColumnCount or eachRows must be greater than 1");
}
if (columnIndex < 0) { if (columnIndex < 0) {
throw new IllegalArgumentException("ColumnIndex must be greater than 0"); throw new IllegalArgumentException("ColumnIndex must be greater than 0");
} }
this.eachRow = eachRow; this.eachRow = eachRow;
this.columnCount = columnCount;
this.columnIndex = columnIndex; this.columnIndex = columnIndex;
} }
@Override @Override
protected void merge(Sheet sheet, Cell cell, Head head, int relativeRowIndex) { protected void merge(Sheet sheet, Cell cell, Head head, int relativeRowIndex) {
if (head.getColumnIndex() == columnIndex && relativeRowIndex % eachRow == 0) { if (head.getColumnIndex() == columnIndex && relativeRowIndex % eachRow == 0) {
CellRangeAddress cellRangeAddress = new CellRangeAddress(cell.getRowIndex(), CellRangeAddress cellRangeAddress = new CellRangeAddress(
cell.getRowIndex() + eachRow - 1, cell.getColumnIndex(), cell.getColumnIndex()); cell.getRowIndex(),
cell.getRowIndex() + eachRow - 1,
cell.getColumnIndex(),
cell.getColumnIndex() + columnCount - 1);
sheet.addMergedRegion(cellRangeAddress); sheet.addMergedRegion(cellRangeAddress);
} }
} }

Loading…
Cancel
Save