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

}
相关推荐
vfvfb3 小时前
bat批量去掉本文件夹中的文件扩展名
服务器·windows·批处理·删除扩展名·bat技巧
沉到海底去吧Go4 小时前
【工具教程】PDF电子发票提取明细导出Excel表格,OFD电子发票行程单提取保存表格,具体操作流程
pdf·excel
开开心心就好6 小时前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
我命由我123459 小时前
VSCode - VSCode 放大与缩小代码
前端·ide·windows·vscode·前端框架·编辑器·软件工具
PT_silver9 小时前
tryhackme——Abusing Windows Internals(进程注入)
windows·microsoft
爱炸薯条的小朋友10 小时前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
Lw老王要学习11 小时前
VScode 使用 git 提交数据到指定库的完整指南
windows·git·vscode
CodeOfCC17 小时前
c语言 封装跨平台线程头文件
linux·c语言·windows
momo卡17 小时前
MinGW-w64的安装详细步骤(c_c++的编译器gcc、g++的windows版,win10、win11真实可用)
c语言·c++·windows
沉到海底去吧Go1 天前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格