easypoi合并单元格工具

package org.jeecgframework.poi.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.jeecgframework.poi.excel.entity.params.MergeEntity;

/**
 * 纵向合并单元格工具类
 * @author JueYue
 * @date 2015年6月21日 上午11:21:40
 */
public final class PoiMergeCellUtil {

    private PoiMergeCellUtil() {
    }

    /**
     * 纵向合并相同内容的单元格
     * 
     * @param sheet
     * @param mergeMap key--列,value--依赖的列,没有传空
     * @param startRow 开始行
     */
    public static void mergeCells(Sheet sheet, Map mergeMap, int startRow) {
        Map mergeDataMap = new HashMap();
        if (mergeMap.size() == 0) {
            return;
        }
        Row row;
        Set sets = mergeMap.keySet();
        String text;
        for (int i = startRow; i <= sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);
            for (Integer index : sets) {
                if (row.getCell(index) == null) {
                    mergeDataMap.get(index).setEndRow(i);
                } else {
                    text = row.getCell(index).getStringCellValue();
                    if (StringUtils.isNotEmpty(text)) {
                        hanlderMergeCells(index, i, text, mergeDataMap, sheet, row.getCell(index),
                            mergeMap.get(index));
                    } else {
                        mergeCellOrContinue(index, mergeDataMap, sheet);
                    }
                }
            }
        }
        if (mergeDataMap.size() > 0) {
            for (Integer index : mergeDataMap.keySet()) {
                sheet.addMergedRegion(new CellRangeAddress(mergeDataMap.get(index).getStartRow(),
                    mergeDataMap.get(index).getEndRow(), index, index));
            }
        }

    }

    /**
     * 处理合并单元格
     * 
     * @param index
     * @param rowNum
     * @param text
     * @param mergeDataMap
     * @param sheet
     * @param cell
     * @param delys
     */
    private static void hanlderMergeCells(Integer index, int rowNum, String text,
                                          Map mergeDataMap, Sheet sheet,
                                          Cell cell, int[] delys) {
        if (mergeDataMap.containsKey(index)) {
            if (checkIsEqualByCellContents(mergeDataMap.get(index), text, cell, delys, rowNum)) {
                mergeDataMap.get(index).setEndRow(rowNum);
            } else {
                sheet.addMergedRegion(new CellRangeAddress(mergeDataMap.get(index).getStartRow(),
                    mergeDataMap.get(index).getEndRow(), index, index));
                mergeDataMap.put(index, createMergeEntity(text, rowNum, cell, delys));
            }
        } else {
            mergeDataMap.put(index, createMergeEntity(text, rowNum, cell, delys));
        }
    }

    /**
     * 字符为空的情况下判断
     * 
     * @param index
     * @param mergeDataMap
     * @param sheet
     */
    private static void mergeCellOrContinue(Integer index, Map mergeDataMap,
                                            Sheet sheet) {
        if (mergeDataMap.containsKey(index)
            && mergeDataMap.get(index).getEndRow() != mergeDataMap.get(index).getStartRow()) {
            sheet.addMergedRegion(new CellRangeAddress(mergeDataMap.get(index).getStartRow(),
                mergeDataMap.get(index).getEndRow(), index, index));
            mergeDataMap.remove(index);
        }
    }

    private static MergeEntity createMergeEntity(String text, int rowNum, Cell cell, int[] delys) {
        MergeEntity mergeEntity = new MergeEntity(text, rowNum, rowNum);
        List list = new ArrayList(delys.length);
        mergeEntity.setRelyList(list);
        for (int i = 0; i < delys.length; i++) {
            list.add(getCellNotNullText(cell, delys[i], rowNum));
        }
        return mergeEntity;
    }

    private static boolean checkIsEqualByCellContents(MergeEntity mergeEntity, String text,
                                                      Cell cell, int[] delys, int rowNum) {
        // 没有依赖关系
        if (delys == null || delys.length == 0) {
            return mergeEntity.getText().equals(text);
        }
        // 存在依赖关系
        if (mergeEntity.getText().equals(text)) {
            for (int i = 0; i < delys.length; i++) {
                if (!getCellNotNullText(cell, delys[i], rowNum).equals(
                    mergeEntity.getRelyList().get(i))) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    /**
     * 获取一个单元格的值,确保这个单元格必须有值,不然向上查询
     * 
     * @param cell
     * @param index
     * @param rowNum
     * @return
     */
    private static String getCellNotNullText(Cell cell, int index, int rowNum) {
        String temp = cell.getRow().getCell(index).getStringCellValue();
        while (StringUtils.isEmpty(temp)) {
            temp = cell.getRow().getSheet().getRow(--rowNum).getCell(index).getStringCellValue();
        }
        return temp;
    }

}

你可能感兴趣的:(java)