Package org.openxml4j.opc

Examples of org.openxml4j.opc.PackagePart


              corePart.getRelationshipsByType(REL);
            Iterator<PackageRelationship> it = prc.iterator();
            if(it.hasNext()) {
                PackageRelationship rel = it.next();
                PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
                PackagePart part = corePart.getPackage().getPart(relName);
                return part.getInputStream();
            } else {
              log.log(POILogger.WARN, "No part " + DEFAULT_NAME + " found");
              return null;
            }
    }
View Full Code Here


            } catch(InvalidFormatException e) {
              throw new IllegalStateException(e);
            }
            PackageRelationship rel =
              corePart.addRelationship(ppName, TargetMode.INTERNAL, REL);
            PackagePart part = corePart.getPackage().createPart(ppName, TYPE);
           
            OutputStream out = part.getOutputStream();
            model.writeTo(out);
            out.close();
           
            return rel.getId();
    }
View Full Code Here

        workbook.write(out);
        out.close();
       
        // Check the package contains what we'd expect it to
        Package pkg = Package.open(file.toString());
        PackagePart wbRelPart =
          pkg.getPart(PackagingURIHelper.createPartName("/xl/_rels/workbook.xml.rels"));
        assertNotNull(wbRelPart);
        assertTrue(wbRelPart.isRelationshipPart());
        assertEquals(ContentTypes.RELATIONSHIPS_PART, wbRelPart.getContentType());
       
        PackagePart wbPart =
          pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
        // Links to the three sheets, shared strings and styles
        assertTrue(wbPart.hasRelationships());
        assertEquals(5, wbPart.getRelationships().size());
       
        // Load back the XSSFWorkbook
        workbook = new XSSFWorkbook(pkg);
        assertEquals(3, workbook.getNumberOfSheets());
        assertNotNull(workbook.getSheetAt(0));
View Full Code Here

    assertNotNull(workbook.getSharedStringSource());
    assertNotNull(workbook.getStylesSource());
   
    // And check a few low level bits too
    Package pkg = Package.open(xml.toString());
        PackagePart wbPart =
          pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
       
        // Links to the three sheets, shared, styles and themes
        assertTrue(wbPart.hasRelationships());
        assertEquals(6, wbPart.getRelationships().size());

    }
View Full Code Here

        File.separator + "WithVariousData.xlsx"
    );
    assertTrue(xml.exists());
     
    Package pkg = Package.open(xml.toString());
    PackagePart cpart = pkg.getPart(
        PackagingURIHelper.createPartName("/xl/comments1.xml")
    );
   
    CommentsTable ct = new CommentsTable(cpart.getInputStream());
    assertEquals(2, ct.getNumberOfComments());
    assertEquals(1, ct.getNumberOfAuthors());

    XSSFComment comment = ct.findCellComment("C5");
   
View Full Code Here

    // 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.wordprocessingml.document.main+xml");

    // Create main document part content
    Document doc = DocumentHelper.createDocument();
    Namespace nsWordprocessinML = new Namespace("w",
        "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
    Element elDocument = doc.addElement(new QName("document",
        nsWordprocessinML));
    Element elBody = elDocument.addElement(new QName("body",
        nsWordprocessinML));
    Element elParagraph = elBody.addElement(new QName("p",
        nsWordprocessinML));
    Element elRun = elParagraph
        .addElement(new QName("r", nsWordprocessinML));
    Element elText = elRun.addElement(new QName("t", nsWordprocessinML));
    elText.setText("Hello Open XML !");

    // Save the XML structure into the part
    StreamHelper.saveXmlInStream(doc, corePart.getOutputStream());

    // Save package
    pkg.close();
  }
View Full Code Here

          .getRelationshipsByType(
              PackageRelationshipTypes.CORE_PROPERTIES)
          .getRelationship(0);

      // Get core properties part from the relationship.
      PackagePart coreDocument = p.getPart(corePropertiesRelationship);

      // Retrieve and display the URI and the content type of the core
      // properties part of the document.
      System.out.println(coreDocument.getPartName() + " -> "
          + coreDocument.getContentType());
    } catch (OpenXML4JException e) {
      e.printStackTrace();
      return;
    }
  }
View Full Code Here

    PackageRelationship rel =
      workbookPart.getRelationship(relId);
    PackagePartName relName =
      PackagingURIHelper.createPartName(rel.getTargetURI());
   
    PackagePart sheet = container.getPart(relName);
    if(sheet == null) {
      throw new OpenXML4JException("No part found for rel " + rel);
    }
    try {
      SAXReader reader = new SAXReader();
      return reader.read(sheet.getInputStream());
    } catch (DocumentException e) {
      throw new OpenXML4JException(e.getMessage());
    } catch (IOException io) {
      throw new OpenXML4JException(io.getMessage());
    }
View Full Code Here

    PackageRelationship coreDocumentRelationship = pkg
        .getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT)
        .getRelationship(0);

    // Get core document part from the relationship.
    PackagePart coreDocumentPart = pkg.getPart(coreDocumentRelationship);

    InputStream inStream = coreDocumentPart.getInputStream();
    SAXReader docReader = new SAXReader();
    Document doc = docReader.read(inStream);

    Namespace namespaceWordProcessingML = new Namespace("w",
        "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
    Element bodyElement = doc.getRootElement().element(
        new QName("body", namespaceWordProcessingML));

    // Retrieves paragraph childs from body element
    List paragraphs = bodyElement.content();

    // Build a new paragraph element
    Element paragraph = DocumentHelper.createElement(new QName("p",
        namespaceWordProcessingML));
    Element run = paragraph.addElement(new QName("r",
        namespaceWordProcessingML));
    Element text = run
        .addElement(new QName("t", namespaceWordProcessingML));
    text.setText("New paragraph added with OpenXML4J !");

    // Add the newly created paragraph at the last position of paragraph
    // elements, just before the w:sectPr element
    paragraphs.add(paragraphs.size() - 1, paragraph);

    // Save back the content into the part
    StreamHelper.saveXmlInStream(doc, coreDocumentPart.getOutputStream());

    pkg.save(new File(demoCore.getTestRootPath() + "sample_output.docx"));
  }
View Full Code Here

      // Gets the core part relationship
      PackageRelationship coreDocRelationship = p.getRelationshipsByType(
          PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);

      // Get core part
      PackagePart corePart = p.getPart(coreDocRelationship);
      return corePart;
    } catch (OpenXML4JException e) {
      DemoCore.getLogger().debug(e.getMessage());
      return null;
    }
View Full Code Here

TOP

Related Classes of org.openxml4j.opc.PackagePart

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.