EasyExcel导出文件合并单元格实际应用

1,763 阅读2分钟

1 复杂头

image.png

/**
 * 社区团购测试汇总数据DTO
 *
 * @date        2021-08-03 17:11:53
 * @author      linyuan
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EastCommunitySummaryDataDTO {

    @ExcelProperty(value = {"华东大区-汇总表","监测日期"}, index = 0)
    private String version;

    @ExcelProperty(value = {"华东大区-汇总表","大区"}, index = 1)
    private String area;

    @ExcelProperty(value = {"华东大区-汇总表","城市"}, index = 2)
    private String city;

    @ExcelProperty(value = {"华东大区-汇总表","责任人"}, index = 3)
    private String header;

    @ExcelProperty(value = {"华东大区-汇总表","商品规格"}, index = 4)
    private String specifications;

    @ExcelProperty(value = {"华东大区-汇总表","是否允许分销"}, index = 5)
    private String isAllow;

    @ExcelProperty(value = {"华东大区-汇总表","多多买菜", "多多监测商品"}, index = 6)
    private String duoDuoGoodsCount;

    @ExcelProperty(value = {"华东大区-汇总表","多多买菜", "多多低价商品"}, index = 7)
    private String duoDuoLowPriceGoodsCount;

    @ExcelProperty(value = {"华东大区-汇总表","多多买菜", "多多最低价"}, index = 8)
    private String duoDuoLowestPrice;

    @ExcelProperty(value = {"华东大区-汇总表","橙心优选", "橙心监测商品"}, index = 9)
    private String chengXinGoodsCount;

    @ExcelProperty(value = {"华东大区-汇总表","橙心优选", "橙心低价商品"}, index = 10)
    private String chengXinLowPriceGoodsCount;

    @ExcelProperty(value = {"华东大区-汇总表","橙心优选", "橙心最低价"}, index = 11)
    private String chengXinLowestPrice;

    @ExcelProperty(value = {"华东大区-汇总表","美团优选", "美团监测商品"}, index = 12)
    private String meiTuanGoodsCount;

    @ExcelProperty(value = {"华东大区-汇总表","美团优选", "美团低价商品"}, index = 13)
    private String meiTuanLowPriceGoodsCount;

    @ExcelProperty(value = {"华东大区-汇总表","美团优选", "美团最低价"}, index = 14)
    private String meiTuanLowestPrice;

    @ExcelProperty(value = {"华东大区-汇总表", "兴盛优选", "兴盛监测商品"}, index = 15)
    private String xingShengGoodsCount;

    @ExcelProperty(value = {"华东大区-汇总表", "兴盛优选", "兴盛低价商品"}, index = 16)
    private String xingShengLowPriceGoodsCount;

    @ExcelProperty(value = {"华东大区-汇总表", "兴盛优选", "兴盛最低价"}, index = 17)
    private String xingShengLowestPrice;

    @ExcelProperty(value = {"华东大区-汇总表", "京喜拼拼", "京喜监测商品"}, index = 18)
    private String jingXiGoodsCount;

    @ExcelProperty(value = {"华东大区-汇总表", "京喜拼拼", "京喜低价商品"}, index = 19)
    private String jingXiLowPriceGoodsCount;

    @ExcelProperty(value = {"华东大区-汇总表", "京喜拼拼", "京喜最低价"}, index = 20)
    private String jingXiLowestPrice;

}

2 单元格合并

/**
 * 自定义单元格合并
 * @author linyuan
 */
public class CustomMergeStrategy extends AbstractMergeStrategy {

    private final Integer index;

    private final List<Integer> cellIndexes;

    private Sheet sheet;

    public CustomMergeStrategy(Integer index, List<Integer> cellIndexes) {
        this.index = index;
        this.cellIndexes = cellIndexes;
    }

    private void mergeGroupColumn(int index) {
        int rowCount = 3;
        for (int count : cellIndexes) {
            if (count > 1) {
                CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCount, rowCount+ count -1, index, index);
                sheet.addMergedRegionUnsafe(cellRangeAddress);
            }
            rowCount += count;
        }
    }


    /**
     * merge
     *
     * @param sheet             sheet
     * @param cell              cell
     * @param head              head
     * @param relativeRowIndex  relativeRowIndex
     */
    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
        this.sheet = sheet;
        mergeGroupColumn(index);
    }
}
  • 初始的rowCount = 3是因为复杂头占用的行数是3。
  • cellIndexes是需要合并的行数的集合。例如:1-3行、4-10行合并的话,里面的元素就是3,7。
  • index是需要合并的列,从0开始