Package org.apache.poi.openxml4j.opc

Examples of org.apache.poi.openxml4j.opc.Package


  /**
   * Test M4.1 rule.
   */
  public void testOnlyOneCorePropertiesPart_AddPart() {
    String sampleFileName = OpenXML4JTestDataSamples.getComplianceSampleFileName("OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
    Package pkg = null;
    try {
      pkg = Package.open(sampleFileName);
    } catch (InvalidFormatException e) {
      throw new RuntimeException(e);
    }
   
    URI partUri = createURI("/docProps/core2.xml");
    try {
      pkg.createPart(PackagingURIHelper.createPartName(partUri),
          ContentTypes.CORE_PROPERTIES_PART);
      fail("expected OPC compliance exception was not thrown");
    } catch (InvalidFormatException e) {
      throw new RuntimeException(e);
    } catch (InvalidOperationException e) {
      // expected during successful test
      assertEquals("OPC Compliance error [M4.1]: you try to add more than one core properties relationship in the package !", e.getMessage());
    }
    pkg.revert();
  }
View Full Code Here


/**
* XSSF and SAX (Event API)
*/
public class FromHowTo {
  public void processOneSheet(String filename) throws Exception {
    Package pkg = Package.open(filename);
    XSSFReader r = new XSSFReader( pkg );
    SharedStringsTable sst = r.getSharedStringsTable();

    XMLReader parser = fetchSheetParser(sst);

View Full Code Here

    parser.parse(sheetSource);
    sheet2.close();
  }

  public void processAllSheets(String filename) throws Exception {
    Package pkg = Package.open(filename);
    XSSFReader r = new XSSFReader( pkg );
    SharedStringsTable sst = r.getSharedStringsTable();
   
    XMLReader parser = fetchSheetParser(sst);
View Full Code Here

*/
public class XSSFTestDataSamples {
  public static final XSSFWorkbook openSampleWorkbook(String sampleName) {
    InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleName);
    try {
      Package pkg = Package.open(is);
      return new XSSFWorkbook(pkg);
    } catch (InvalidFormatException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
View Full Code Here

                File tmp = File.createTempFile("poi-ooxml-", ".xlsx");
                tmp.deleteOnExit();
                FileOutputStream out = new FileOutputStream(tmp);
                wb.write(out);
                out.close();
          Package pkg = Package.open(tmp.getAbsolutePath());
          result = new XSSFWorkbook(pkg);
        } else {
          throw new RuntimeException("Unexpected workbook type ("
              + wb.getClass().getName() + ")");
        }
View Full Code Here

    /**
     * Create a new SpreadsheetML package and setup the default minimal content
     */
    protected static Package newPackage() {
        try {
            Package pkg = Package.create(PackageHelper.createTempFile());
            // Main part
            PackagePartName corePartName = PackagingURIHelper.createPartName(XWPFRelation.DOCUMENT.getDefaultFileName());
            // Create main part relationship
            pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
            // Create main document part
            pkg.createPart(corePartName, XWPFRelation.DOCUMENT.getContentType());

            pkg.getPackageProperties().setCreatorProperty("Apache POI");

            return pkg;
        } catch (Exception e){
            throw new POIXMLException(e);
        }
View Full Code Here

    assertTrue(sampleFile.exists());
    assertTrue(complexFile.exists());
  }

  public void testContainsMainContentType() throws Exception {
    Package pack = POIXMLDocument.openPackage(sampleFile.toString());
   
    boolean found = false;
    for(PackagePart part : pack.getParts()) {
      if(part.getContentType().equals(XWPFRelation.DOCUMENT.getContentType())) {
        found = true;
      }
      System.out.println(part);
    }
View Full Code Here

     */
    public static Package clone(Package pkg, File file) throws OpenXML4JException, IOException {

        String path = file.getAbsolutePath();

        Package dest = Package.create(path);
        PackageRelationshipCollection rels = pkg.getRelationships();
        for (PackageRelationship rel : rels) {
            PackagePart part = pkg.getPart(rel);
            PackagePart part_tgt;
            if (rel.getRelationshipType().equals(PackageRelationshipTypes.CORE_PROPERTIES)) {
                copyProperties(pkg.getPackageProperties(), dest.getPackageProperties());
                continue;
            } else {
                dest.addRelationship(part.getPartName(), rel.getTargetMode(), rel.getRelationshipType());
                part_tgt = dest.createPart(part.getPartName(), part.getContentType());
            }

            OutputStream out = part_tgt.getOutputStream();
            IOUtils.copy(part.getInputStream(), out);
            out.close();

            if(part.hasRelationships()) {
                copy(pkg, part, dest, part_tgt);
            }
        }
        dest.close();

        //the temp file will be deleted when JVM terminates
        new File(path).deleteOnExit();
        return Package.open(path);
    }
View Full Code Here

      );
  }
   
    public void testGetBits() throws Exception {
      File f = new File(dirName, "SampleSS.xlsx");
      Package pkg = Package.open(f.toString());
     
      XSSFReader r = new XSSFReader(pkg);
     
      assertNotNull(r.getWorkbookData());
      assertNotNull(r.getSharedStringsData());
View Full Code Here

      assertNotNull(r.getStylesTable());
    }
   
    public void testStyles() throws Exception {
      File f = new File(dirName, "SampleSS.xlsx");
      Package pkg = Package.open(f.toString());
     
      XSSFReader r = new XSSFReader(pkg);
     
      assertEquals(3, r.getStylesTable().getFonts().size());
      assertEquals(0, r.getStylesTable()._getNumberFormatSize());
View Full Code Here

TOP

Related Classes of org.apache.poi.openxml4j.opc.Package

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.