EasyExcel custom merge

1. The desired result

insert image description here

Second, export the dto class

@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;

}

Here, id is used as the basis for judging whether to merge

3. Customize the merge strategy

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;
    }
}


Fourth, the controller layer code

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);
        }

insert image description here

Guess you like

Origin blog.csdn.net/qq_44798321/article/details/130425576