Excel属性值添加和获取-可对其唯一方案,让其对应Excel能直接找到对应的模板

java操作Excel属性值(自定义参数)

    /**
     * 写入导入模板ID到Excel自定义文档属性。
     *
     * @param workbook   工作簿
     * @param templateId 模板id
     */
    public static void setImportTemplateIdProperty(Workbook workbook, Long templateId) {
        if (!(workbook instanceof XSSFWorkbook xssfWorkbook) || templateId == null) {
            return;
        }
        POIXMLProperties.CustomProperties customProperties = xssfWorkbook.getProperties().getCustomProperties();
        if (customProperties.contains(IMPORT_TEMPLATE_ID_PROPERTY_NAME)) {
            customProperties.getProperty(IMPORT_TEMPLATE_ID_PROPERTY_NAME).setLpwstr(String.valueOf(templateId));
            return;
        }
        customProperties.addProperty(IMPORT_TEMPLATE_ID_PROPERTY_NAME, String.valueOf(templateId));
    }

    /**
     * 读取Excel自定义文档属性中的导入模板ID。
     *
     * @param workbook 工作簿
     * @return 模板id
     */
    public static Long getImportTemplateIdProperty(Workbook workbook) {
        if (!(workbook instanceof XSSFWorkbook xssfWorkbook)) {
            return null;
        }
        CTProperty property = xssfWorkbook.getProperties()
                .getCustomProperties()
                .getProperty(IMPORT_TEMPLATE_ID_PROPERTY_NAME);
        if (property == null) {
            return null;
        }
        String value = property.isSetLpwstr() ? property.getLpwstr() : property.getLpstr();
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return Long.valueOf(value);
    }

Excel属性值查看位置

image

 

posted @ 2026-08-17 11:31  骚哥  阅读(4)  评论(0)    收藏  举报