public POIXMLProperties(OPCPackage docPackage) { this.pkg = docPackage; // Core properties core = new CoreProperties((PackagePropertiesPart)pkg.GetPackageProperties()); // Extended properties PackageRelationshipCollection extRel = pkg.GetRelationshipsByType(PackageRelationshipTypes.EXTENDED_PROPERTIES); if (extRel.Size == 1) { extPart = pkg.GetPart(extRel.GetRelationship(0)); ExtendedPropertiesDocument props = ExtendedPropertiesDocument.Parse( extPart.GetInputStream() ); ext = new ExtendedProperties(props); } else { extPart = null; ext = new ExtendedProperties((ExtendedPropertiesDocument)NEW_EXT_INSTANCE.Copy()); } // Custom properties PackageRelationshipCollection custRel = pkg.GetRelationshipsByType(PackageRelationshipTypes.CUSTOM_PROPERTIES); if (custRel.Size == 1) { custPart = pkg.GetPart(custRel.GetRelationship(0)); CustomPropertiesDocument props = CustomPropertiesDocument.Parse( custPart.GetInputStream() ); cust = new CustomProperties(props); } else { custPart = null; cust = new CustomProperties((CustomPropertiesDocument)NEW_CUST_INSTANCE.Copy()); } }
public void TestCustomProperties() { POIXMLDocument wb = new XSSFWorkbook(); CustomProperties customProps = wb.GetProperties().CustomProperties; customProps.AddProperty("test-1", "string val"); customProps.AddProperty("test-2", 1974); customProps.AddProperty("test-3", 36.6); //Adding a duplicate try { customProps.AddProperty("test-3", 36.6); Assert.Fail("expected exception"); } catch (ArgumentException e) { Assert.AreEqual("A property with this name already exists in the custom properties", e.Message); } customProps.AddProperty("test-4", true); wb = (XSSFWorkbook)XSSFTestDataSamples.WriteOutAndReadBack((XSSFWorkbook)wb); CT_CustomProperties ctProps = wb.GetProperties().CustomProperties.GetUnderlyingProperties(); Assert.AreEqual(6, ctProps.sizeOfPropertyArray()); CT_Property p; p = ctProps.GetPropertyArray(0); Assert.AreEqual("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.fmtid); Assert.AreEqual("test-1", p.name); Assert.AreEqual("string val", p.Item.ToString()); Assert.AreEqual(2, p.pid); p = ctProps.GetPropertyArray(1); Assert.AreEqual("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.fmtid); Assert.AreEqual("test-2", p.name); Assert.AreEqual(1974, p.Item); Assert.AreEqual(3, p.pid); p = ctProps.GetPropertyArray(2); Assert.AreEqual("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.fmtid); Assert.AreEqual("test-3", p.name); Assert.AreEqual(36.6, p.Item); Assert.AreEqual(4, p.pid); p = ctProps.GetPropertyArray(3); Assert.AreEqual("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", p.fmtid); Assert.AreEqual("test-4", p.name); Assert.AreEqual(true, p.Item); Assert.AreEqual(5, p.pid); p = ctProps.GetPropertyArray(4); Assert.AreEqual("Generator", p.name); Assert.AreEqual("NPOI", p.Item); Assert.AreEqual(6, p.pid); //p = ctProps.GetPropertyArray(5); //Assert.AreEqual("Generator Version", p.name); //Assert.AreEqual("2.0.9", p.Item); //Assert.AreEqual(7, p.pid); }