LoopMergeStrategy.java 1.2 KB
Newer Older
Z
zhuangjiaju 已提交
1 2 3 4 5 6 7 8 9 10
package com.alibaba.excel.write.merge;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import com.alibaba.excel.metadata.Head;

/**
 * The regions of the loop merge
Z
zhuangjiaju 已提交
11
 *
Z
zhuangjiaju 已提交
12
 * @author Jiaju Zhuang
Z
zhuangjiaju 已提交
13 14 15
 */
public class LoopMergeStrategy extends AbstractMergeStrategy {
    private int eachRow;
Z
zhuangjiaju 已提交
16
    private int columnIndex;
Z
zhuangjiaju 已提交
17

Z
zhuangjiaju 已提交
18 19 20 21 22 23
    public LoopMergeStrategy(int eachRow, int columnIndex) {
        if (eachRow < 1) {
            throw new IllegalArgumentException("EachRows must be greater than 1");
        }
        if (columnIndex < 0) {
            throw new IllegalArgumentException("ColumnIndex must be greater than 0");
Z
zhuangjiaju 已提交
24 25
        }
        this.eachRow = eachRow;
Z
zhuangjiaju 已提交
26
        this.columnIndex = columnIndex;
Z
zhuangjiaju 已提交
27 28 29 30
    }

    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, int relativeRowIndex) {
Z
zhuangjiaju 已提交
31
        if (head.getColumnIndex() == columnIndex && relativeRowIndex % eachRow == 0) {
Z
zhuangjiaju 已提交
32
            CellRangeAddress cellRangeAddress = new CellRangeAddress(cell.getRowIndex(),
Z
zhuangjiaju 已提交
33
                cell.getRowIndex() + eachRow - 1, cell.getColumnIndex(), cell.getColumnIndex());
Z
zhuangjiaju 已提交
34 35 36 37
            sheet.addMergedRegion(cellRangeAddress);
        }
    }
}