easypoi 导出Excel 使用总结

easypoi 导出Excel

导出Excel需要设置标题,且标题是多行,标题下面是列表头

设置表格标题

java 复制代码
 ExportParams headExportParams = new ExportParams();
 StringBuilder buffer = new StringBuilder("");
 buffer.append("1、课程名称:......\n")
         .append("2、课程标签:......\n")
         .append("5、适用人群:......");
 headExportParams.setTitle(buffer.toString());
 headExportParams.setTitleHeight((short) 50);

设置标题样式

java 复制代码
headExportParams.setStyle(MyExcelExportStyler.class);

完整代码如下

java 复制代码
@Override
@Async
public void downloadCoursewareDataAsync(List<Long> ids, FileDownloadHistory history, String sheetName) {
    try {
        history.setExportStatus(1);
        List<CoursewareManagement> list = coursewareManagementMapper.queryCoursewareByIdList(ids);
        List<CoursewareManagementDto> voList = new ArrayList<>();
		
        // 创建参数对象(用来设定excel得sheet得内容等信息)
        ExportParams headExportParams = new ExportParams();
        StringBuilder buffer = new StringBuilder("");
        buffer.append("1、课程名称:......,不允许重名\n")
                .append("2、课程标签:多个标签用 、 隔开\n")
                .append("5、适用人群-岗位:......");
        headExportParams.setTitle(buffer.toString());
        headExportParams.setTitleHeight((short) 50);
        headExportParams.setStyle(MyExcelExportStyler.class);
        // 设置sheet得名称
        headExportParams.setSheetName(sheetName);
        // 创建sheet1使用得map
        Map<String, Object> headExportMap = new HashMap<>();
        // title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName
        headExportMap.put("title", headExportParams);
        // 模版导出对应得实体类型
        headExportMap.put("entity", CoursewareManagementDto.class);
        // sheet中要填充得数据
        headExportMap.put("data", voList);

        // 将sheet1、sheet2、sheet3使用得map进行包装
        List<Map<String, Object>> sheetsList = new ArrayList<>();
        sheetsList.add(headExportMap);

        try (
                Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.XSSF);
                FileOutputStream outputStream = new FileOutputStream(history.getFilePath());
        ) {
            workbook.write(outputStream);
        } catch (Exception e) {
            throw e;
        }
    } catch (Exception e) {
        log.error("课件管理异步下载接口==>{}", e.getLocalizedMessage(), e);
        history.setExportStatus(0);
    }
    history.setModifyTime(new Date());
    fileDownloadHistoryMapper.updateByPrimaryKeySelective(history);
}

public class MyExcelExportStyler extends ExcelExportStylerDefaultImpl {

    public MyExcelExportStyler(Workbook workbook) {
        super(workbook);
    }

    /**
     * <p>
     * 设置表格标题样式
     * </p>
     */
    @Override
    public CellStyle getHeaderStyle(short color) {
        CellStyle titleStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontHeightInPoints((short) 12);
        titleStyle.setFont(font);
        titleStyle.setWrapText(true);
        titleStyle.setAlignment(HorizontalAlignment.LEFT);
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        return titleStyle;
    }
}

源码里的 Excel 导出基本注释很重要,特别是按实体类导出的时候

可以看下这个类 cn.afterturn.easypoi.excel.annotation.Excel

@Excel needMerge 和 @ExcelCollection 配合使用,处理 Excel 一对多的关系

java 复制代码
@Data
public class CoursewareManagementVo implements IExcelModel, IExcelDataModel {

    @Excel(name = "课件名称", needMerge = true, width = 25, orderNum = "1")
    private String coursewareName;
    @Excel(name = "课件格式", needMerge = true, width = 25, orderNum = "2")
    private String coursewareFormat;
    @Excel(name = "创建时间", needMerge = true, width = 25, orderNum = "3", format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    @Excel(name = "课程条线", needMerge = true, width = 25, orderNum = "4")
    private String classifyName;
    @ExcelCollection(name = "", orderNum = "5")
    private List<ColumnVo> columnVos;

    private String errorMsg;
    private Integer rowNum;

    @Override
    public Integer getRowNum() {
        return rowNum;
    }

    @Override
    public void setRowNum(Integer integer) {
        this.rowNum = integer;
    }

    @Override
    public String getErrorMsg() {
        return errorMsg;
    }

    @Override
    public void setErrorMsg(String s) {
        this.errorMsg = s;
    }

}
相关推荐
集成显卡40 分钟前
windows 下使用 bat 批处理运行 Chrome 无头模式刷一波访问量
windows·程序员
葡萄城技术团队1 天前
从100秒到10秒的性能优化,你真的掌握 Excel 的使用技巧了吗?
excel
QQ3596773452 天前
ArcGIS Pro实现基于 Excel 表格批量创建标准地理数据库(GDB)——高效数据库建库解决方案
数据库·arcgis·excel
路由侠内网穿透2 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式3 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
带娃的IT创业者3 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
csdn_aspnet3 天前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠3 天前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu3 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化
Source.Liu3 天前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化