Пример #1
0
        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());
            }
        }
Пример #2
0
        protected void Rebase(OPCPackage pkg)
        {
            PackageRelationshipCollection relationshipsByType = this.packagePart.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");

            if (relationshipsByType.Size != 1)
            {
                throw new InvalidOperationException("Tried to rebase using http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument but found " + (object)relationshipsByType.Size + " parts of the right type");
            }
            this.packageRel  = relationshipsByType.GetRelationship(0);
            this.packagePart = this.packagePart.GetRelatedPart(this.packageRel);
        }
Пример #3
0
        /**
         * When you open something like a theme, call this to
         *  re-base the XML Document onto the core child of the
         *  current core document
         */
        protected void Rebase(OPCPackage pkg)
        {
            PackageRelationshipCollection cores =
                packagePart.GetRelationshipsByType(coreDocumentRel);

            if (cores.Size != 1)
            {
                throw new InvalidOperationException(
                          "Tried to rebase using " + coreDocumentRel +
                          " but found " + cores.Size + " parts of the right type"
                          );
            }
            packagePart = packagePart.GetRelatedPart(cores.GetRelationship(0));
        }
Пример #4
0
        public void TestContentType()
        {
            String filepath = OpenXml4NetTestDataSamples.GetSampleFileName("sample.docx");
            // Retrieves core properties part
            OPCPackage p = OPCPackage.Open(filepath, PackageAccess.READ);
            PackageRelationshipCollection rels = p.GetRelationshipsByType(PackageRelationshipTypes.CORE_PROPERTIES);
            PackageRelationship           corePropertiesRelationship = rels.GetRelationship(0);
            PackagePart coreDocument = p.GetPart(corePropertiesRelationship);

            Assert.AreEqual("application/vnd.openxmlformats-package.core-properties+xml", coreDocument.ContentType);
            // TODO - finish writing this test
            Assume.That(false, "finish writing this test");

            ContentTypeManager ctm = new ZipContentTypeManager(coreDocument.GetInputStream(), p);
        }
Пример #5
0
        /**
         * When you open something like a theme, call this to
         *  re-base the XML Document onto the core child of the
         *  current core document
         */
        protected void Rebase(OPCPackage pkg)
        {
            PackageRelationshipCollection cores =
                packagePart.GetRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);

            if (cores.Size != 1)
            {
                throw new InvalidOperationException(
                          "Tried to rebase using " + PackageRelationshipTypes.CORE_DOCUMENT +
                          " but found " + cores.Size + " parts of the right type"
                          );
            }
            packageRel  = cores.GetRelationship(0);
            packagePart = POIXMLDocument.GetTargetPart(pkg, packageRel);
        }
Пример #6
0
        public void TestSetVBAProject()
        {
            FileInfo file;

            byte[] allBytes = new byte[256];
            for (int i = 0; i < 256; i++)
            {
                allBytes[i] = (byte)(i - 128);
            }

            XSSFWorkbook wb1 = new XSSFWorkbook();

            wb1.CreateSheet();
            wb1.SetVBAProject(new ByteArrayInputStream(allBytes));
            file = TempFile.CreateTempFile("poi-", ".xlsm");
            Stream out1 = new FileStream(file.FullName, FileMode.Open, FileAccess.ReadWrite);

            wb1.Write(out1);
            out1.Close();
            wb1.Close();


            // Check the package contains what we'd expect it to
            OPCPackage  pkg    = OPCPackage.Open(file);
            PackagePart wbPart = pkg.GetPart(PackagingUriHelper.CreatePartName("/xl/workbook.xml"));

            Assert.IsTrue(wbPart.HasRelationships);
            PackageRelationshipCollection relationships = wbPart.Relationships.GetRelationships(XSSFRelation.VBA_MACROS.Relation);

            Assert.AreEqual(1, relationships.Size);
            Assert.AreEqual(XSSFRelation.VBA_MACROS.DefaultFileName, relationships.GetRelationship(0).TargetUri.ToString());
            PackagePart vbaPart = pkg.GetPart(PackagingUriHelper.CreatePartName(XSSFRelation.VBA_MACROS.DefaultFileName));

            Assert.IsNotNull(vbaPart);
            Assert.IsFalse(vbaPart.IsRelationshipPart);
            Assert.AreEqual(XSSFRelation.VBA_MACROS.ContentType, vbaPart.ContentType);
            byte[] fromFile = IOUtils.ToByteArray(vbaPart.GetInputStream());
            CollectionAssert.AreEqual(allBytes, fromFile);
            // Load back the XSSFWorkbook just to check nothing explodes
            XSSFWorkbook wb2 = new XSSFWorkbook(pkg);

            Assert.AreEqual(1, wb2.NumberOfSheets);
            Assert.AreEqual(XSSFWorkbookType.XLSM, wb2.WorkbookType);
            pkg.Close();
        }
Пример #7
0
        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());
            }
        }
Пример #8
0
        public void TestLoadRelationships()
        {
            Stream     is1 = OpenXml4NetTestDataSamples.OpenSampleStream("sample.xlsx");
            OPCPackage pkg = OPCPackage.Open(is1);

            logger.Log(POILogger.DEBUG, "1: " + pkg);
            PackageRelationshipCollection rels = pkg.GetRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);
            PackageRelationship           coreDocRelationship = rels.GetRelationship(0);
            PackagePart corePart = pkg.GetPart(coreDocRelationship);

            String[] relIds = { "rId1", "rId2", "rId3" };
            foreach (String relId in relIds)
            {
                PackageRelationship rel = corePart.GetRelationship(relId);
                Assert.IsNotNull(rel);
                PackagePartName relName   = PackagingUriHelper.CreatePartName(rel.TargetUri);
                PackagePart     sheetPart = pkg.GetPart(relName);
                Assert.AreEqual(1, sheetPart.Relationships.Size, "Number of relationships1 for " + sheetPart.PartName);
            }
        }
Пример #9
0
        public void TestCreate()
        {
            XSSFWorkbook       workbook     = new XSSFWorkbook();
            XSSFSheet          sheet        = workbook.CreateSheet() as XSSFSheet;
            XSSFRow            row          = sheet.CreateRow(0) as XSSFRow;
            XSSFCreationHelper CreateHelper = workbook.GetCreationHelper() as XSSFCreationHelper;

            String[] urls =
            {
                "http://apache.org/",
                "www.apache.org",
                "/temp",
                "file:///c:/temp",
                "http://apache.org/default.php?s=isTramsformed&submit=Search&la=*&li=*"
            };
            for (int i = 0; i < urls.Length; i++)
            {
                String        s    = urls[i];
                XSSFHyperlink link = CreateHelper.CreateHyperlink(HyperlinkType.Url) as XSSFHyperlink;
                link.Address = (s);

                XSSFCell cell = row.CreateCell(i) as XSSFCell;
                cell.Hyperlink = (link);
            }
            workbook = XSSFTestDataSamples.WriteOutAndReadBack(workbook) as XSSFWorkbook;
            sheet    = workbook.GetSheetAt(0) as XSSFSheet;
            PackageRelationshipCollection rels = sheet.GetPackagePart().Relationships;

            Assert.AreEqual(urls.Length, rels.Size);
            for (int i = 0; i < rels.Size; i++)
            {
                PackageRelationship rel = rels.GetRelationship(i);
                if (rel.TargetUri.IsAbsoluteUri && rel.TargetUri.IsFile)
                {
                    Assert.AreEqual(urls[i].Replace("file:///", "").Replace("/", "\\"), rel.TargetUri.LocalPath);
                }
                else
                {
                    // there should be a relationship for each URL
                    Assert.AreEqual(urls[i], rel.TargetUri.ToString());
                }
            }

            // Bugzilla 53041: Hyperlink relations are duplicated when saving XSSF file
            workbook = XSSFTestDataSamples.WriteOutAndReadBack(workbook) as XSSFWorkbook;
            sheet    = workbook.GetSheetAt(0) as XSSFSheet;
            rels     = sheet.GetPackagePart().Relationships;
            Assert.AreEqual(urls.Length, rels.Size);
            for (int i = 0; i < rels.Size; i++)
            {
                PackageRelationship rel = rels.GetRelationship(i);
                if (rel.TargetUri.IsAbsoluteUri && rel.TargetUri.IsFile)
                {
                    Assert.AreEqual(urls[i].Replace("file:///", "").Replace("/", "\\"), rel.TargetUri.LocalPath);
                }
                else
                {
                    // there should be a relationship for each URL
                    Assert.AreEqual(urls[i], rel.TargetUri.ToString());
                }
            }
        }
Пример #10
0
        //[Ignore("add relation Uri #Sheet1!A1")]
        public void TestCreatePackageWithCoreDocument()
        {
            MemoryStream baos = new MemoryStream();
            OPCPackage   pkg  = OPCPackage.Create(baos);

            // Add a core document
            PackagePartName corePartName = PackagingUriHelper.CreatePartName("/xl/workbook.xml");

            // Create main part relationship
            pkg.AddRelationship(corePartName, TargetMode.Internal, PackageRelationshipTypes.CORE_DOCUMENT, "rId1");
            // Create main document part
            PackagePart corePart = pkg.CreatePart(corePartName, "application/vnd.Openxmlformats-officedocument.spreadsheetml.sheet.main+xml");
            // Put in some dummy content
            Stream coreOut = corePart.GetOutputStream();

            byte[] buffer = Encoding.UTF8.GetBytes("<dummy-xml />");
            coreOut.Write(buffer, 0, buffer.Length);
            coreOut.Close();

            // And another bit
            PackagePartName     sheetPartName = PackagingUriHelper.CreatePartName("/xl/worksheets/sheet1.xml");
            PackageRelationship rel           =
                corePart.AddRelationship(sheetPartName, TargetMode.Internal, "http://schemas.Openxmlformats.org/officeDocument/2006/relationships/worksheet", "rSheet1");

            Assert.IsNotNull(rel);

            PackagePart part = pkg.CreatePart(sheetPartName, "application/vnd.Openxmlformats-officedocument.spreadsheetml.worksheet+xml");

            Assert.IsNotNull(part);

            // Dummy content again
            coreOut = corePart.GetOutputStream();
            buffer  = Encoding.UTF8.GetBytes("<dummy-xml2 />");
            coreOut.Write(buffer, 0, buffer.Length);
            coreOut.Close();

            //add a relationship with internal target: "#Sheet1!A1"
            corePart.AddRelationship(PackagingUriHelper.ToUri("#Sheet1!A1"), TargetMode.Internal, "http://schemas.Openxmlformats.org/officeDocument/2006/relationships/hyperlink", "rId2");

            // Check things are as expected
            PackageRelationshipCollection coreRels =
                pkg.GetRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);

            Assert.AreEqual(1, coreRels.Size);
            PackageRelationship coreRel = coreRels.GetRelationship(0);

            Assert.IsNotNull(coreRel);
            Assert.AreEqual("/", coreRel.SourceUri.ToString());
            Assert.AreEqual("/xl/workbook.xml", coreRel.TargetUri.ToString());
            Assert.IsNotNull(pkg.GetPart(coreRel));


            // Save and re-load
            pkg.Close();
            FileInfo   tmp  = TempFile.CreateTempFile("testCreatePackageWithCoreDocument", ".zip");
            FileStream fout = new FileStream(tmp.FullName, FileMode.Create, FileAccess.ReadWrite);

            try
            {
                buffer = baos.ToArray();
                fout.Write(buffer, 0, buffer.Length);
            }
            finally
            {
                fout.Close();
            }
            pkg = OPCPackage.Open(tmp.FullName);
            //tmp.Delete();

            try
            {
                // Check still right
                coreRels = pkg.GetRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);
                Assert.AreEqual(1, coreRels.Size);
                coreRel = coreRels.GetRelationship(0);
                Assert.IsNotNull(coreRel);

                Assert.AreEqual("/", coreRel.SourceUri.ToString());
                Assert.AreEqual("/xl/workbook.xml", coreRel.TargetUri.ToString());
                corePart = pkg.GetPart(coreRel);
                Assert.IsNotNull(corePart);

                PackageRelationshipCollection rels = corePart.GetRelationshipsByType("http://schemas.Openxmlformats.org/officeDocument/2006/relationships/hyperlink");
                Assert.AreEqual(1, rels.Size);
                rel = rels.GetRelationship(0);
                Assert.IsNotNull(rel);
                Assert.Warn(" 'Sheet1!A1' and rel.TargetUri.Fragment should be equal.");
                //Assert.AreEqual("Sheet1!A1", rel.TargetUri.Fragment);

                assertMSCompatibility(pkg);
            }
            finally
            {
                pkg.Close();
            }

            Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files.");
        }