From 6ecdb90abdabc19a299399ced7db544d8049bd47 Mon Sep 17 00:00:00 2001 From: zl Date: Fri, 25 Oct 2019 15:07:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BE=93=E5=87=BA=E6=97=B6=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=A0=BC=E5=A2=9E=E5=8A=A0=E8=A1=8C=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/write/merge/LoopMergeStrategy.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java b/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java index 85694a8..b087804 100644 --- a/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java +++ b/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 { private int eachRow; + private int columnCount; private int columnIndex; public LoopMergeStrategy(int eachRow, int columnIndex) { + this(eachRow, 1, columnIndex); + } + + public LoopMergeStrategy(int eachRow, int columnCount, int columnIndex) { if (eachRow < 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) { throw new IllegalArgumentException("ColumnIndex must be greater than 0"); } this.eachRow = eachRow; + this.columnCount = columnCount; this.columnIndex = columnIndex; } @Override protected void merge(Sheet sheet, Cell cell, Head head, int relativeRowIndex) { if (head.getColumnIndex() == columnIndex && relativeRowIndex % eachRow == 0) { - CellRangeAddress cellRangeAddress = new CellRangeAddress(cell.getRowIndex(), - cell.getRowIndex() + eachRow - 1, cell.getColumnIndex(), cell.getColumnIndex()); + CellRangeAddress cellRangeAddress = new CellRangeAddress( + cell.getRowIndex(), + cell.getRowIndex() + eachRow - 1, + cell.getColumnIndex(), + cell.getColumnIndex() + columnCount - 1); sheet.addMergedRegion(cellRangeAddress); } }