EasyExcel生成多sheet页的excel

一、controller层

java 复制代码
@ApiOperation(value = "明细查询导出")
@PostMapping(value = "/SummaryDetailExport")
public void summaryDetailExport(@RequestBody SearchDTO dto, HttpServletResponse response) throws IOException {
    reportService.deptPackagingSummaryDetailExport(dto, response);
}

二、serviceimpl

java 复制代码
@Override
    public void deptPackagingSummaryDetailExport(SearchDTO dto, HttpServletResponse response) throws IOException {
        // 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
        List<DeptSummaryReq> exportVos = configPackageTaskMapper.DeptPackagingSummary(dto);
        DecimalFormat df = new DecimalFormat("#.00");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("科室打包明细查询" + ".xlsx", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        ExcelWriter excelWriter = EasyExcel.write(outputStream).build();
        //在根据科室id去查询有哪些包,以及价格
        for (int i = 0; i < exportVos.size(); i++) {
            dto.setDeptId(exportVos.get(i).getDeptId());
            List<DeptSummaryReq> detail = configPackageTaskMapper.queryDeptPackagingDetailBydeptId(dto);
            for (DeptSummaryReq regDetail : detail) {
                if (regDetail.getPrice() == null) {
                    regDetail.setAmount(0.00);
                    regDetail.setPrice(0.00);
                } else {
                    regDetail.setAmount(BigDecimal.valueOf(regDetail.getPrice()).multiply(BigDecimal.valueOf(regDetail.getPackageCount())).doubleValue());
                }
            }

            DeptSummaryReq regDetail1 = new DeptSummaryReq();
            regDetail1.setPackageCode(dto.getStartTime() + "    " + dto.getEndTime() + "    打印时间:" + DateUtil.now());
            DeptSummaryReq regDetail2 = new DeptSummaryReq();
            if (dto.getPackageType() == null) {
                List<String> typeName = recoverPackagesMapper.queryPackageType();
                regDetail2.setPackageCode("包类型:" + String.join(",", typeName));
            } else {
                regDetail2.setPackageCode("包类型:" + String.join(",", dto.getPackageName()));
            }
            DeptSummaryReq regDetail = new DeptSummaryReq();
            regDetail.setPackageCode("科室数量合计:    " + detail.stream().mapToInt(DeptSummaryReq::getPackageCount).sum()
                    + "       科室金额合计:   ¥" + Double.valueOf(df.format(detail.stream().mapToDouble(DeptSummaryReq::getAmount).sum())));
            detail.add(regDetail);
            detail.add(regDetail1);
            detail.add(regDetail2);
            //创建合并策略
            //这里我们上面手动给我们的list添加了3条数据
            //这3天数据是需要合并单元格显示的,所以我们这里需要new 3个OnceAbsoluteMergeStrategy
            // 前面2个数字代表的是行,后面两个代表从那一列开始,到那一列结束
            //比如我们要展示五个字段,那么就是0,4;从0开始到4就一共5列
            OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(detail.size() + 1, detail.size() + 1, 0, 4);
            OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy1 = new OnceAbsoluteMergeStrategy(detail.size(), detail.size(), 0, 4);
            OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy2 = new OnceAbsoluteMergeStrategy(detail.size() - 1, detail.size() - 1, 0, 4);

            //创建一个sheet
            WriteSheet writeSheet = EasyExcel.writerSheet(i, exportVos.get(i).getDeptName())
                    .head(DeptPackagingSummaryDetailExcel.class)
                    //.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    .registerWriteHandler(ExcelUtil.writeCenterStyle())
                    .registerWriteHandler(onceAbsoluteMergeStrategy)
                    .registerWriteHandler(onceAbsoluteMergeStrategy1)
                    .registerWriteHandler(onceAbsoluteMergeStrategy2)
                    .build();

            //将sheet写入excelwriter
            excelWriter.write(detail, writeSheet);
        }
        if (excelWriter != null) {
            excelWriter.finish();
        }
    }

三、entity

DeptSummaryReq
java 复制代码
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.annotation.write.style.HeadStyle;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.cloud.common.core.annotation.ExcelParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Data
@ColumnWidth(25)//设置列宽
@HeadRowHeight(20)//设置行高
@EqualsAndHashCode
@HeadStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER)//表头样式
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER)//内容样式
@ApiModel(value="DeptSummaryReq", description="导出出参")
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DeptSummaryReq {
    private static final long serialVersionUID=1L;

    @ApiModelProperty(value = "包编码")
    @ExcelProperty({"明细报表", "包编码"})
    private String packageCode;

    @ApiModelProperty(value = "包名称")
    @ExcelProperty({"明细报表", "包名称"})
    private String packageName;

    @ApiModelProperty(name = "数量")
    @ExcelProperty({"明细报表", "数量"})
    private int packageCount;

    @ApiModelProperty(name = "单价")
    @ExcelProperty({"明细报表", "单价"})
    private Double price;

    @ApiModelProperty(name = "金额")
    @ExcelProperty({"明细报表", "金额"})
    private Double  amount;

    @ApiModelProperty(name = "科室id")
    @ExcelIgnore
    private Integer deptId;

    @ApiModelProperty(name = "科室名称")
    @ExcelIgnore
    private String deptName;

}

四、效果

相关推荐
夕除几秒前
spring boot 6
java·spring boot·后端
johnrui24 分钟前
JUC之AQS
java·开发语言·jvm
Full Stack Developme25 分钟前
Spring 模块介绍
java·后端·spring
多敲代码防脱发38 分钟前
Spring进阶(BeanFactory与ApplicationContext)
java·数据库·spring boot·后端·spring
吴声子夜歌1 小时前
Java——反射
java·反射
JAVA面经实录9171 小时前
完整版JVM 深度学习体系(二)
java·jvm
.ZGR.1 小时前
线程池相关知识及并发统计案例实现
java·开发语言
慕言手记1 小时前
IDEA 插件常用-2026版
java·ide·spring boot·intellij-idea·idea·intellij idea
颖火虫盟主1 小时前
Hello World MCP Server 实现总结
java·前端·python
iiiiyu1 小时前
⾯向对象和集合编程题
java·大数据·开发语言·数据结构·编程语言