public POIXMLProperties(OPCPackage docPackage) { this.pkg = docPackage; this.core = new POIXMLProperties.CoreProperties((PackagePropertiesPart)this.pkg.GetPackageProperties()); PackageRelationshipCollection relationshipsByType1 = this.pkg.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"); if (relationshipsByType1.Size == 1) { this.extPart = this.pkg.GetPart(relationshipsByType1.GetRelationship(0)); this.ext = new POIXMLProperties.ExtendedProperties(ExtendedPropertiesDocument.Parse(this.extPart.GetInputStream())); } else { this.extPart = (PackagePart)null; this.ext = new POIXMLProperties.ExtendedProperties(POIXMLProperties.NEW_EXT_INSTANCE.Copy()); } PackageRelationshipCollection relationshipsByType2 = this.pkg.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties"); if (relationshipsByType2.Size == 1) { this.custPart = this.pkg.GetPart(relationshipsByType2.GetRelationship(0)); this.cust = new POIXMLProperties.CustomProperties(CustomPropertiesDocument.Parse(this.custPart.GetInputStream())); } else { this.custPart = (PackagePart)null; this.cust = new POIXMLProperties.CustomProperties(POIXMLProperties.NEW_CUST_INSTANCE.Copy()); } }
public void TestCustomProperties() { POIXMLDocument wb = new XSSFWorkbook(); POIXMLProperties.CustomProperties customProps = wb.GetProperties().GetCustomProperties(); 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().GetCustomProperties().GetUnderlyingProperties(); Assert.AreEqual(4, 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); }