【问题标题】:apache poi read custom propertyapache poi 读取自定义属性
【发布时间】:2014-09-03 11:22:21
【问题描述】:

我已向我的工作簿对象添加了一个自定义属性,例如:

((XSSFWorkbook)workBook).getProperties().getCustomProperties().addProperty("fileNameSuffix" , "testName");

现在我怎样才能再读一遍。
为什么没有getProperty(String key)这样的方法?

【问题讨论】:

    标签: apache-poi custom-properties


    【解决方案1】:

    你的意思是喜欢POIXMLProperties.CustomProperties.getProperty(String)这个方法吗?我认为这应该做你想要的。好吧,假设您使用的 Apache POI 版本足够新,至少可以拥有它!

    但是,请注意它返回一个 CTProperty 对象,该对象相当低级,并且没有明确的类型。你必须调用各种isSetXXX 方法来确定它是什么类型,然后getXXX 来获取值。

    POIXMLPropertiesTextExtractor 中有一个如何做到这一点的示例

    【讨论】:

      【解决方案2】:

      我想出了一个办法,但我不是很喜欢这种方式

          List<CTProperty> customProperties = workBook.getProperties().getCustomProperties().getUnderlyingProperties().getPropertyList();
          String fileNameSuffix = "";
          for(int i = 0 ; i < customProperties.size() ; i++) {
              CTProperty property = customProperties.get(i);
              if (customProperties.get(i).getName().equals("testName"))
                  fileNameSuffix = property.getLpwstr(); // getLpwstr() will return the value of the property
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多