EasyExcel 导出冻结指定行

导出的实体类

package org.jeecg.modules.eis.test;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

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

/**
 * EasyExcel 导出测试类
 */
@Getter
@Setter
public class CheckItemDTO {

    @ContentStyle(horizontalAlignment = HorizontalAlignment.LEFT) // 设置内容文本左对齐
    @ContentFontStyle(fontHeightInPoints = 12) // 设置内容字体大小
    @HeadStyle(fillForegroundColor = 13) // 设置表头背景色
    @HeadFontStyle(fontHeightInPoints = 16) // 设置表头字体大小
    @ColumnWidth(12) // 设置宽度为20
    @ExcelProperty(value = "编码", index = 0)
    private String code;

    @ContentStyle(horizontalAlignment = HorizontalAlignment.LEFT) // 设置内容文本左对齐
    @ContentFontStyle(fontHeightInPoints = 12) // 设置内容字体大小
    @HeadStyle(fillForegroundColor = 13) // 设置表头背景色
    @HeadFontStyle(fontHeightInPoints = 16) // 设置表头字体大小
    @ColumnWidth(20) // 设置宽度为20
    @ExcelProperty(value = "名称", index = 1)
    private String name;

    @ContentStyle(horizontalAlignment = HorizontalAlignment.LEFT) // 设置内容文本左对齐
    @ContentFontStyle(fontHeightInPoints = 12) // 设置内容字体大小
    @HeadStyle(fillForegroundColor = 13) // 设置表头背景色
    @HeadFontStyle(fontHeightInPoints = 16) // 设置表头字体大小
    @ColumnWidth(20) // 设置宽度为30
    @ExcelProperty(value = "价格", index = 2)
    private BigDecimal price;

    @ContentStyle(horizontalAlignment = HorizontalAlignment.LEFT) // 设置内容文本左对齐
    @ContentFontStyle(fontHeightInPoints = 12) // 设置内容字体大小
    @HeadStyle(fillForegroundColor = 13) // 设置表头背景色
    @HeadFontStyle(fontHeightInPoints = 16) // 设置表头字体大小
    @ColumnWidth(20) // 设置宽度为30
    @ExcelProperty(value = "模态", index = 3)
    private String modality;

    @ContentStyle(horizontalAlignment = HorizontalAlignment.LEFT) // 设置内容文本左对齐
    @ContentFontStyle(fontHeightInPoints = 12) // 设置内容字体大小
    @HeadStyle(fillForegroundColor = 13) // 设置表头背景色
    @HeadFontStyle(fontHeightInPoints = 16) // 设置表头字体大小
    @ColumnWidth(30) // 设置宽度为30
    @ExcelProperty(value = "项目归属", index = 4)
    private String belong;


    public static List<CheckItemDTO> data() {
        int count = 50;
        List<CheckItemDTO> list = new ArrayList<>(count);

        for (int i = 0; i < count; i++) {
            int idx = (i + 1);
            CheckItemDTO e = new CheckItemDTO();
            e.setCode(String.valueOf(idx));
            e.setName("这是名称_" + idx);
            e.setPrice(BigDecimal.valueOf(100).add(new BigDecimal(idx)));
            e.setModality((idx % 2 == 0) ? "CT" : "MR");
            e.setBelong("格林蓝德");

            list.add(e);
        }

        return list;
    }
}

接口方法

    @RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
    public void downloadExcel(HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            outputStream = response.getOutputStream();
            //冻结指定行:SheetWriteHandler 接口重对 afterSheetCreate 进行处理
            EasyExcel.write(outputStream, CheckItemDTO.class).sheet("模板").registerWriteHandler(new SheetWriteHandler() {
                @Override
                public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
                }

                @Override
                public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
                    Sheet sheet = writeSheetHolder.getSheet();
                    sheet.createFreezePane(1, 1); //冻结第一列,第一行
                }
            }).doWrite(CheckItemDTO.data());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(outputStream); //关闭流。这里用到了 hutool 工具类
        }
    }

实际效果

在这里插入图片描述

参考

https://blog.youkuaiyun.com/qq_38974638/article/details/117197652

EasyExcel github demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值