使用easyexcel 导出多级表头demo

先看效果:

1、引入maven依赖

<!--EasyExcel -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.2.1</version>
</dependency>        

2、实体类

package com.huawei.it.domain;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;

/**
 * @description: 嫁功率考勤实体类
 * @author: yeyong
 * @create: 2024-01-16
 */
public class Jglkq{

    @ExcelProperty({"考勤状态"})
    private String kqzt; //考勤状态

    @ExcelProperty({"日期"})
    @ColumnWidth(20)
    private String rq; //日期

    @ExcelProperty({"工号"})
    private String gh; //工号

    @ExcelProperty({"姓名"})
    private String xm; //姓名

    @ExcelProperty({"应出勤"})
    private String ycq; //应出勤

    @ExcelProperty({"实出勤"})
    private String scq; //实出勤

    @ExcelProperty({"班制名称"})
    private String bzmc;  //班制名称

    @ExcelProperty({"班一","上"})
    private String sw; //上

    @ExcelProperty({"班一","下"})
    private String xw; //下

    @ExcelProperty({"平时加班"})
    private String psjb; //平时加班

    @ExcelProperty({"假日加班"})
    private String jrjb; //假日加班

    @ExcelProperty({"周六排班8小时"})
    private String str1; //周六排班8小时

    @ExcelProperty({"周六超8小时"})
    private String str2; //周六超8小时

    //忽略这个字段
    @ExcelIgnore
    private String IsNight;  //是否夜班  0:否  1:是



    public String getKqzt() {
        return kqzt;
    }

    public void setKqzt(String kqzt) {
        this.kqzt = "正常";
    }

    public String getRq() {
        return rq;
    }

    public void setRq(String rq) {
        this.rq = rq;
    }

    public String getGh() {
        return gh;
    }

    public void setGh(String gh) {
        this.gh = gh;
    }

    public String getXm() {
        return xm;
    }

    public void setXm(String xm) {
        this.xm = xm;
    }

    public String getYcq() {
        return ycq;
    }

    public void setYcq(String ycq) {
        this.ycq = ycq;
    }

    public String getScq() {
        return scq;
    }

    public void setScq(String scq) {
        this.scq = scq;
    }

    public String getBzmc() {
        return bzmc;
    }

    public void setBzmc(String bzmc) {
        this.bzmc = bzmc;
    }

    public String getSw() {
        return sw;
    }

    public void setSw(String sw) {
        this.sw = sw;
    }

    public String getXw() {
        return xw;
    }

    public void setXw(String xw) {
        this.xw = xw;
    }

    public String getPsjb() {
        return psjb;
    }

    public void setPsjb(String psjb) {
        this.psjb = psjb;
    }

    public String getJrjb() {
        return jrjb;
    }

    public void setJrjb(String jrjb) {
        this.jrjb = jrjb;
    }

    public String getStr1() {
        return str1;
    }

    public void setStr1(String str1) {
        this.str1 = str1;
    }

    public String getStr2() {
        return str2;
    }

    public void setStr2(String str2) {
        this.str2 = str2;
    }

    public String getIsNight() {
        return IsNight;
    }

    public void setIsNight(String isNight) {
        IsNight = isNight;
    }

}

3、web导出 controller

package com.huawei.web.controller.it;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.huawei.common.core.controller.BaseController;
import com.huawei.common.core.page.TableDataInfo;
import com.huawei.common.utils.poi.MonthSheetWriteHandler;
import com.huawei.it.domain.DemoData;
import com.huawei.it.domain.Jglkq;
import com.huawei.it.service.JglkqService;
import org.apache.poi.ss.usermodel.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: yeyong
 * @create: 2024-01-16
 */
@Controller
@RequestMapping("/hr/jglkq")
public class JglkqController extends BaseController {
    private String prefix = "it/hr";
    @Autowired
    private JglkqService jglkqService;

 //使用easyExcel导出
    @GetMapping("/export")
    public void easyExcelExport(HttpServletResponse response,Jglkq jglkq){
        //备注:使用easyExcel导出多级表头
        try {
            //准备数据
            List<Jglkq> kqList =new ArrayList<Jglkq>();
            List<Jglkq> list = jglkqService.selectJglkqList(jglkq);
            for (Jglkq kq1 : list) {
                Jglkq kq = new Jglkq();
                kq.setKqzt(kq1.getKqzt());  //考勤状态
                kq.setRq(kq1.getRq()); //日期
                kq.setGh(kq1.getGh());  //工号
                kq.setXm(kq1.getXm());  //姓名
                kq.setYcq("1");  //应出勤
                kq.setScq("1");  //实出勤
                kq.setBzmc(kq1.getBzmc());  //班制名称
                kq.setSw(kq1.getSw());  //上班
                kq.setXw(kq1.getXw());   //下班
                kq.setPsjb(kq1.getPsjb());  //平时加班
                kq.setJrjb(kq1.getJrjb());  //假日加班
                kq.setStr1(kq1.getStr1());  //
                kq.setStr2(kq1.getStr2());  //
                kqList.add(kq);
            }

            //response.setContentType("application/vnd.ms-excel");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            //StringBuffer bigTitle = new StringBuffer("考勤结果明细查询");
            //String fileName = URLEncoder.encode(bigTitle.toString(), "UTF-8")+ ".xlsx";
            String fileName = System.currentTimeMillis()+".xlsx";
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);

            // 头的策略
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            // 默认设置为水平居中
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
            contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
            contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
            contentWriteCellStyle.setBorderTop(BorderStyle.THIN);

            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), Jglkq.class)
                    //.autoCloseStream(Boolean.FALSE).registerWriteHandler(horizontalCellStyleStrategy)
                    .sheet("考勤结果明细查询")
                    .relativeHeadRowIndex(1)
                     //设置拦截器或自定义样式
                    .registerWriteHandler(new MonthSheetWriteHandler())
                    .registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle,contentWriteCellStyle))
                    .doWrite(kqList);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}

4、在拦截器中对表头的样式进行自定义的覆盖

package com.huawei.common.utils.poi;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * @description:  表头设置拦截器
 * @author: yeyong
 * @create: 2024-01-17
 */
public class MonthSheetWriteHandler implements SheetWriteHandler {

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder)   {
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);
        Row row1 = sheet.createRow(0);
        row1.setHeight((short) 800);
        Cell cell = row1.createCell(0);
        //设置标题
        cell.setCellValue("考勤结果明细查询" );
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeight((short) 400);
        font.setFontName("宋体");
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);
        sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, 15));
    }

}

可以使用EasyExcel来实现多级表头导出EasyExcel是一个基于Java的简单易用的Excel操作工具,支持大数据量的导入导出操作。 下面是一个示例代码,演示了如何实现多级表头导出: ```java // 创建一个excel写对象 ExcelWriter writer = EasyExcel.write("output.xlsx").build(); // 定义表头数据 List<List<String>> head = new ArrayList<>(); // 第一行表头 List<String> headRow1 = new ArrayList<>(); headRow1.add("一级表头"); headRow1.add("一级表头"); headRow1.add("一级表头"); head.add(headRow1); // 第二行表头 List<String> headRow2 = new ArrayList<>(); headRow2.add("二级表头"); headRow2.add("二级表头"); headRow2.add("二级表头"); head.add(headRow2); // 第三行表头 List<String> headRow3 = new ArrayList<>(); headRow3.add("三级表头"); headRow3.add("三级表头"); headRow3.add("三级表头"); head.add(headRow3); // 写入表头数据 Sheet sheet = new Sheet(1, 0); sheet.setHead(head); writer.write1(null, sheet); // 写入内容数据(省略) // 关闭excel写对象 writer.finish(); ``` 在上面的示例代码中,我们创建了一个ExcelWriter对象,并指定了输出文件名为"output.xlsx"。然后,我们定义了一个包含多级表头表头数据,每一级的表头都是一个List<String>。我们将这些表头数据添加到head列表中,然后使用Sheet对象将head列表设置为excel的表头。最后,调用writer的write1方法写入表头数据。 你可以根据需要修改示例代码中的表头数据,然后使用EasyExcel提供的其他方法写入内容数据。完成后,调用writer的finish方法关闭excel写对象即可。 希望对你有帮助!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值