easyExcel导出表格,按需合并单元格

1、前言

旨在记录easyExcel下载文件

2、准备

private String getPath() {
    return String.valueOf(System.currentTimeMillis());
}

3、将数据源直接导出

模板类

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * @author jei
 * @description /
 * @date 2022-08-31
 **/
@Data
@AllArgsConstructor
@ColumnWidth(15)
public class CollectOrderVo implements Serializable {

    @ApiModelProperty(value = "下单时间")
    @ExcelProperty(value = "下单时间")
    @ColumnWidth(20)
    private String orderTime;

    @ApiModelProperty(value = "就餐日期")
    @ExcelProperty(value = "就餐日期")
    private String diningDay;

    @ApiModelProperty(value = "就餐时间 0-11:30 1-12:00 2-17:30")
    @ExcelIgnore
    private Integer diningTime;

    @ApiModelProperty(value = "时间")
    @ExcelProperty(value = "时间")
    private String time;

    @ApiModelProperty(value = "备注")
    @ExcelProperty(value = "备注")
    private String remark;

    @ApiModelProperty(value = "菜品名称")
    @ExcelProperty(value = "菜品名称")
    private String dishName;

    @ApiModelProperty(value = "份数")
    @ExcelProperty(value = "份数")
    private Integer allCount;

}

impl实现类

@Override
public void downloadDetailExcel(List<CollectOrderVo> queryDetailList, HttpServletResponse response) throws IOException {
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setCharacterEncoding("utf-8");
    String fileName = URLEncoder.encode(this.getPath(), "UTF-8");
    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

    EasyExcel.write(response.getOutputStream(), CollectOrderVo.class).sheet().doWrite(queryDetailList);

}

4、将数据源按要求合并单元格导出

模板类

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * @author jei
 * @description /
 * @date 2022-08-31
 **/
@Data
@AllArgsConstructor
@ColumnWidth(15)
public class CollectHaveDIshVo implements Serializable {

    @ApiModelProperty(value = "订单编号")
    @ExcelIgnore
    private Integer orderId;

    @ApiModelProperty(value = "就餐日期")
    @ExcelProperty(value = "就餐日期")
    private String diningDay;

    @ApiModelProperty(value = "就餐时间")
    @ExcelIgnore
    private Integer diningTime;

    @ApiModelProperty(value = "就餐时间")
    @ExcelProperty(value = "时间")
    private String time;

    @ApiModelProperty(value = "订单人姓名")

    @ExcelProperty(value = "订单人姓名")
    private String userName;

    @ApiModelProperty(value = "部门")
    @ExcelProperty(value = "部门")
    private String deptName;

    @ApiModelProperty(value = "订单金额")
    @ExcelProperty(value = "订单金额")
    private String eposAmount;

    @ApiModelProperty(value = "订单详情编号")
    @ExcelIgnore
    private Integer detailId;

    @ApiModelProperty(value = "菜品名称")
    @ExcelProperty(value = "菜品名称")
    private String dishName;

    @ApiModelProperty(value = "菜品金额")
    @ExcelProperty(value = "菜品金额")
    private String detailAmount;

}

CustomMergeStrategy类

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.ArrayList;
import java.util.List;

// 自定义合并策略 该类继承了AbstractMergeStrategy抽象合并策略,需要重写merge()方法
public class CustomMergeStrategy extends AbstractMergeStrategy {

    /**
     * 分组,每几行合并一次
     */
    private List<Integer> exportFieldGroupCountList;

    /**
     * 目标合并列index
     */
    private Integer targetColumnIndex;

    // 需要开始合并单元格的首行index
    private Integer rowIndex;

    // exportDataList为待合并目标列的值
    public CustomMergeStrategy(List<String> exportDataList, Integer targetColumnIndex) {
        this.exportFieldGroupCountList = getGroupCountList(exportDataList);
        this.targetColumnIndex = targetColumnIndex;
    }


    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {

        if (null == rowIndex) {
            rowIndex = cell.getRowIndex();
        }
        // 仅从首行以及目标列的单元格开始合并,忽略其他
        if (cell.getRowIndex() == rowIndex && cell.getColumnIndex() == targetColumnIndex) {
            mergeGroupColumn(sheet);
        }
    }

    private void mergeGroupColumn(Sheet sheet) {
        int rowCount = rowIndex;
        for (Integer count : exportFieldGroupCountList) {
            if (count == 1) {
                rowCount += count;
                continue;
            }
            // 合并单元格
            CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCount, rowCount + count - 1, targetColumnIndex, targetColumnIndex);
            sheet.addMergedRegionUnsafe(cellRangeAddress);
            rowCount += count;
        }
    }

    // 该方法将目标列根据值是否相同连续可合并,存储可合并的行数
    private List<Integer> getGroupCountList(List<String> exportDataList) {
        if (CollectionUtils.isEmpty(exportDataList)) {
            return new ArrayList<>();
        }

        List<Integer> groupCountList = new ArrayList<>();
        int count = 1;

        for (int i = 1; i < exportDataList.size(); i++) {
            if (exportDataList.get(i).equals(exportDataList.get(i - 1))) {
                count++;
            } else {
                groupCountList.add(count);
                count = 1;
            }
        }
        // 处理完最后一条后
        groupCountList.add(count);
        return groupCountList;
    }
}

impl实现类

@Override
public void downExcel(List<CollectHaveDIshVo> queryOrderJoinDetailList, HttpServletResponse response) throws Exception {
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("utf-8");
    String fileName = URLEncoder.encode(this.getPath(), "UTF-8");
    response.setHeader("content-type", "application/octet-stream");
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

    ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(response.getOutputStream(), CollectHaveDIshVo.class)
            .registerWriteHandler(new CustomMergeStrategy(queryOrderJoinDetailList.stream().map(x -> x.getOrderId().toString()).collect(Collectors.toList()), 0))
            .registerWriteHandler(new CustomMergeStrategy(queryOrderJoinDetailList.stream().map(x -> x.getOrderId().toString()).collect(Collectors.toList()), 1))
            .registerWriteHandler(new CustomMergeStrategy(queryOrderJoinDetailList.stream().map(x -> x.getOrderId().toString()).collect(Collectors.toList()), 2))
            .registerWriteHandler(new CustomMergeStrategy(queryOrderJoinDetailList.stream().map(x -> x.getOrderId().toString()).collect(Collectors.toList()), 3))
            .registerWriteHandler(new CustomMergeStrategy(queryOrderJoinDetailList.stream().map(x -> x.getOrderId().toString()).collect(Collectors.toList()), 4));
    excelWriterBuilder.sheet().doWrite(queryOrderJoinDetailList);

}
posted @   南翔技校毕业后  阅读(778)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示