EasyExcel自定义合并

文章介绍了如何使用Java来创建一个DTO类用于数据导出到Excel,并应用了自定义的单元格合并策略。DTO类包含各种属性,如医务室名称、盘点日期等,并使用了注解来处理Excel的格式和转换。此外,还定义了一个名为ExcelMergeStrategy的合并策略类,用于根据特定条件合并单元格。在Controller层,实现了导出功能,包括设置文件名、内容样式以及执行合并策略和数据写入。
摘要由CSDN通过智能技术生成

一、想要的结果

在这里插入图片描述

二、导出dto类

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER)//内容样式 水平居中
@ApiModel("盘点明细导出响应体")
public class DetailsExportDto {

//    @ExcelProperty(value = "主键id")
    @ExcelIgnore
    private Integer inventoryTakingId;

    @ExcelProperty(value = "医务室名称")
    private String createClinicName;

    @ExcelProperty(value = "盘点日期",converter = ConvertToExcelDate.class)
    private Date createTime;

    @ExcelProperty(value = "盘点人")
    private String createUserName;

    @JsonIgnore
    @ExcelIgnore
    private Integer stockType;

    @ExcelProperty(value = "物资分类")
    private String stockTypeString;

    @ExcelProperty(value = "物资编号")
    private String materialNumber;

    @JsonIgnore
    @ExcelIgnore
    private Integer productId;

    @ExcelProperty(value = "物资名称")
    private String productName;

    @ExcelProperty(value = "物资类型")
    private String materialTypeString;

    @ExcelProperty(value = "包装规格")
    private String specification;

    @ExcelProperty(value = "库存单位")
    private String packageUnit;

    @ExcelProperty(value = "批次序号")
    private Integer batchNumber;

    @ExcelProperty(value = "有效期至",converter = ConvertToExcelDate.class)
    private Date expiryDate;

    @ExcelProperty(value = "生产产商")
    private String manufacturer;

    @ExcelProperty(value = "总库存")
    private BigDecimal stockNum;

    @ExcelProperty(value = "账面可用库存")
    private BigDecimal availableStockNum;

    @ExcelProperty(value = "实盘可用库存")
    private BigDecimal realNum;

    @ExcelProperty(value = "盘盈盘亏")
    private BigDecimal totalNum;

    @JsonIgnore
    @ExcelIgnore
    private BigDecimal profitNum;

    @JsonIgnore
    @ExcelIgnore
    private BigDecimal lossesNum;

}

这里用id作为是否合并的判断依据

三、自定义合并策略

package com.onehealthpro.oemr.modules.clinicinventory.untils;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
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;

public class ExcelMergeStrategy extends AbstractMergeStrategy {

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

    /**
     * 合并的目标列
     */
    private List<Integer> targetCells;

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

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


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

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

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

    }

    // 该方法将目标列根据值是否相同连续可合并,存储可合并的行数
    private List<Integer> getGroupCountList(List<Integer> 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;
    }
}


四、controller层代码

AccountDto account = OemsContext.getAccount();
        List<DetailsExportDto> list = clinicStockService.detailsExport(account.getClinicId(),req);

        try (final ServletOutputStream os = response.getOutputStream()) {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream;charset=UTF-8");
            String clinicName = account.getClinicName();
//            String clinicName = "ssss";
            String dateString = DateUtils.format(new Date(),"yyyyMMdd");
            final String encodeFileName = URLEncoder.encode( clinicName + "-" + "物资盘点明细" + "-" + dateString + ".xlsx", "UTF-8");
            response.setHeader("Content-Disposition", String.format("attachment;filename=%s", encodeFileName));
            //需要合并的列
            List<Integer> targetCells = new ArrayList<>();
            targetCells.add(0);//第一列
            targetCells.add(1);//第二列
            targetCells.add(2);//第三列

            //内容样式
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            WriteFont contentWriteFont = new WriteFont();
            contentWriteFont.setFontHeightInPoints((short) 9);
            //设置字体样式
            contentWriteFont.setFontName("Arial");
            contentWriteCellStyle.setWriteFont(contentWriteFont);
            contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            //表头和表格内容样式策略
            HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                    new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
            EasyExcel.write(os)
                    .registerWriteHandler(new ExcelMergeStrategy(list.stream().map(DetailsExportDto::getInventoryTakingId).collect(Collectors.toList()), targetCells))
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    .head(DetailsExportDto.class)
                    .sheet(0)
                    .doWrite(list);
        } catch (Exception e) {
            throw new BizException("导出问卷数据失败", e);
        }

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐睡睡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值