引入依赖:

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>${easypoi.version}</version>
</dependency>

使用easypoi导出Pdf 根据官方文档说明需要调用

public class PdfExportUtil {
    public PdfExportUtil() {
    }

    public static Document exportPdf(PdfExportParams entity, Class<?> pojoClass, Collection<?> dataSet, OutputStream outStream) {
        return (new PdfExportServer(outStream, entity)).createPdf(entity, pojoClass, dataSet);
    }

    public static Document exportPdf(PdfExportParams entity, List<ExcelExportEntity> entityList, Collection<? extends Map<?, ?>> dataSet, OutputStream outStream) {
        return (new PdfExportServer(outStream, entity)).createPdfByExportEntity(entity, entityList, dataSet);
    }
}

第一个导出方法,是需要一个pojoclass,作为导出pdf的导出格式。

pojoclass的属性需要加注解@excel(name=""),才能导出,不然会报错

源码:

public void getAllExcelField(String[] exclusions, String targetId, Field[] fields, List<ExcelExportEntity> excelParams, Class<?> pojoClass, List<Method> getMethods, ExcelEntity excelGroup) throws Exception {
    List<String> exclusionsList = exclusions != null ? Arrays.asList(exclusions) : null;

    for(int i = 0; i < fields.length; ++i) {
        Field field = fields[i];
        if (!PoiPublicUtil.isNotUserExcelUserThis(exclusionsList, field, targetId)) {
            if (field.getAnnotation(Excel.class) != null) {
                Excel excel = (Excel)field.getAnnotation(Excel.class);
                String name = PoiPublicUtil.getValueByTargetId(excel.name(), targetId, (String)null);
                if (StringUtils.isNotBlank(name)) {
                    excelParams.add(this.createExcelExportEntity(field, targetId, pojoClass, getMethods, excelGroup));
                }
            } else if (PoiPublicUtil.isCollection(field.getType())) {
                ExcelCollection excel = (ExcelCollection)field.getAnnotation(ExcelCollection.class);
                ParameterizedType pt = (ParameterizedType)field.getGenericType();
                Class<?> clz = (Class)pt.getActualTypeArguments()[0];
                List<ExcelExportEntity> list = new ArrayList();
                this.getAllExcelField(exclusions, StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, PoiPublicUtil.getClassFields(clz), list, clz, (List)null, (ExcelEntity)null);
                ExcelExportEntity excelEntity = new ExcelExportEntity();
                excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, (String)null));
                if (this.i18nHandler != null) {
                    excelEntity.setName(this.i18nHandler.getLocaleName(excelEntity.getName()));
                }

                excelEntity.setOrderNum(Integer.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0")));
                excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName()));
                excelEntity.setList(list);
                excelParams.add(excelEntity);
            } else {
                List<Method> newMethods = new ArrayList();
                if (getMethods != null) {
                    newMethods.addAll(getMethods);
                }

                newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName()));
                ExcelEntity excel = (ExcelEntity)field.getAnnotation(ExcelEntity.class);
                if (excel.show() && StringUtils.isEmpty(excel.name())) {
                    throw new ExcelExportException("if use ExcelEntity ,name mus has value ,data: " + ReflectionToStringBuilder.toString(excel), ExcelExportEnum.PARAMETER_ERROR);
                }

                this.getAllExcelField(exclusions, StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, PoiPublicUtil.getClassFields(field.getType()), excelParams, field.getType(), newMethods, excel.show() ? excel : null);
            }
        }
    }

}

第二个导出方法,是根据List<ExcelExportEntity> entityList导出,指定导出pdf表格的头部

示例:

List<ExcelExportEntity> entityList=new ArrayList<>(cloumns.size());
for (ExclBaseCloumn cloumn : cloumns) {
    ExcelExportEntity entity = new ExcelExportEntity();
    entity.setName(cloumn.getValue());
    entity.setKey(cloumn.getKey());
    entityList.add(entity);
}
fileName = fileName + PDF;
String filePath = FileUtil.getDownloadPath() + "/" + UUID.randomUUID().toString() + "/";
File file = new File(filePath);
if (!file.exists()) {
    file.mkdirs();
}

PdfExportParams params = new PdfExportParams();
FileOutputStream fos;
try {
    fos = new FileOutputStream(filePath + fileName);
    log.info("导出pdf文件位置:{}",filePath + fileName);
} catch (Exception e) {
    log.error("导出pdf文件失败,e:{}", e.getMessage());
    throw new CustomException(ErrorCode.SYS_CUSTOM_ERR, "导出pdf文件失败");
}
Rectangle pageSize= new RectangleReadOnly((595.0F/4)*cloumns.size(), (842.0F/4)*cloumns.size());
params.setStyler(new PdfExportStylerImpl(pageSize));
PdfExportUtil.exportPdf(params,entityList, dataSet, fos);
try {
    filePath = URLEncoder.encode(
            Base64Utils.encodeToString(
                    filePath.getBytes(StandardCharsets.UTF_8)), "utf-8");
} catch (Exception e) {
    log.error("导出文件地址base64出错:" + e.getMessage(), e);
    throw new CustomException(ErrorCode.SYS_KEY_PARAM_ERR);
}
return filePath;
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐