Quantcast
Channel: NPOI
Viewing all articles
Browse latest Browse all 1621

Updated Wiki: 4.2 基于POIXMLDocument创建自定义属性

$
0
0

作者:Tony Qu

在Office 97-2003的文件格式里面中,我们是用SummaryInformation和DocumentSummaryInformation来设置自定义属性的。但OpenXml格式使用了完全不同的极致。

NPOI中,管理OpenXml格式的核心类叫做POIXmlDocument,这和POIFSFileSystem是非常类似的,担负着文件系统管理的职责。而POIXmlDocument.GetProperties则是我们需要关注的重点,它返回的POIXMLProperties实例就是我们需要的自定义属性。

由于XSSFWorkbook和XWPFDocument都继承自POIXmlDocument,所以你真正需要调用的是XSSFWorkbook.GetProperties()和XWPFDocument.GetProperties()。

以下代码是用来设置Custom1自定义属性的:

            if (!workbook.GetProperties().CustomProperties.Contains("Custom1"))
                workbook.GetProperties().CustomProperties.AddProperty("Custom1", "NPOI Demo");

为了防止重复添加同名自定义属性,一定要记得判断它是否已存在。要注意,此操作必须在workbook.Write()之前调用。


Viewing all articles
Browse latest Browse all 1621

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>