Search in sources :

Example 46 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class XSSFWorkbook method commit.

@Override
protected void commit() throws IOException {
    saveNamedRanges();
    saveCalculationChain();
    XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
    xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorkbook.type.getName().getNamespaceURI(), "workbook"));
    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    workbook.save(out, xmlOptions);
    out.close();
}
Also used : QName(javax.xml.namespace.QName) XmlOptions(org.apache.xmlbeans.XmlOptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 47 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class TestPackagingURIHelper method testCreatePartNameRelativeString.

/**
	 * Test createPartName(String, y)
	 */
public void testCreatePartNameRelativeString() throws InvalidFormatException {
    PackagePartName partNameToValid = PackagingURIHelper.createPartName("/word/media/image1.gif");
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    // Base part
    PackagePartName nameBase = PackagingURIHelper.createPartName("/word/document.xml");
    PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
    // Relative part name
    PackagePartName relativeName = PackagingURIHelper.createPartName("media/image1.gif", partBase);
    assertTrue("The part name must be equal to " + partNameToValid.getName(), partNameToValid.equals(relativeName));
    pkg.revert();
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 48 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class TestXSSFBSharedStringsTable method testBasic.

@Test
public void testBasic() throws Exception {
    OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("51519.xlsb"));
    List<PackagePart> parts = pkg.getPartsByName(Pattern.compile("/xl/sharedStrings.bin"));
    assertEquals(1, parts.size());
    XSSFBSharedStringsTable rtbl = new XSSFBSharedStringsTable(parts.get(0));
    List<String> strings = rtbl.getItems();
    assertEquals(49, strings.size());
    assertEquals("コメント", rtbl.getEntryAt(0));
    assertEquals("日本オラクル", rtbl.getEntryAt(3));
    assertEquals(55, rtbl.getCount());
    assertEquals(49, rtbl.getUniqueCount());
//TODO: add in tests for phonetic runs
}
Also used : PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 49 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class TestXSSFBugs method bug45431.

/**
     * We should carry vba macros over after save
     */
@Test
public void bug45431() throws IOException, InvalidFormatException {
    XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("45431.xlsm");
    OPCPackage pkg1 = wb1.getPackage();
    assertTrue(wb1.isMacroEnabled());
    // Check the various macro related bits can be found
    PackagePart vba = pkg1.getPart(PackagingURIHelper.createPartName("/xl/vbaProject.bin"));
    assertNotNull(vba);
    // And the drawing bit
    PackagePart drw = pkg1.getPart(PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml"));
    assertNotNull(drw);
    // Save and re-open, both still there
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
    pkg1.close();
    wb1.close();
    OPCPackage pkg2 = wb2.getPackage();
    assertTrue(wb2.isMacroEnabled());
    vba = pkg2.getPart(PackagingURIHelper.createPartName("/xl/vbaProject.bin"));
    assertNotNull(vba);
    drw = pkg2.getPart(PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml"));
    assertNotNull(drw);
    // And again, just to be sure
    XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
    pkg2.close();
    wb2.close();
    OPCPackage pkg3 = wb3.getPackage();
    assertTrue(wb3.isMacroEnabled());
    vba = pkg3.getPart(PackagingURIHelper.createPartName("/xl/vbaProject.bin"));
    assertNotNull(vba);
    drw = pkg3.getPart(PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml"));
    assertNotNull(drw);
    pkg3.close();
    wb3.close();
}
Also used : SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 50 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class TestXSSFWorkbook method testSetVBAProject.

/**
     * Tests that we can save a workbook with macros and reload it.
     */
@Test
public void testSetVBAProject() throws Exception {
    File file;
    final 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");
    OutputStream out = new FileOutputStream(file);
    wb1.write(out);
    out.close();
    wb1.close();
    // Check the package contains what we'd expect it to
    OPCPackage pkg = OPCPackage.open(file.toString());
    PackagePart wbPart = pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
    assertTrue(wbPart.hasRelationships());
    final PackageRelationshipCollection relationships = wbPart.getRelationships().getRelationships(XSSFRelation.VBA_MACROS.getRelation());
    assertEquals(1, relationships.size());
    assertEquals(XSSFRelation.VBA_MACROS.getDefaultFileName(), relationships.getRelationship(0).getTargetURI().toString());
    PackagePart vbaPart = pkg.getPart(PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName()));
    assertNotNull(vbaPart);
    assertFalse(vbaPart.isRelationshipPart());
    assertEquals(XSSFRelation.VBA_MACROS.getContentType(), vbaPart.getContentType());
    final byte[] fromFile = IOUtils.toByteArray(vbaPart.getInputStream());
    assertArrayEquals(allBytes, fromFile);
    // Load back the XSSFWorkbook just to check nothing explodes
    @SuppressWarnings("resource") XSSFWorkbook wb2 = new XSSFWorkbook(pkg);
    assertEquals(1, wb2.getNumberOfSheets());
    assertEquals(XSSFWorkbookType.XLSM, wb2.getWorkbookType());
    pkg.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) MemoryPackagePart(org.apache.poi.openxml4j.opc.internal.MemoryPackagePart) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Aggregations

PackagePart (org.apache.poi.openxml4j.opc.PackagePart)118 OutputStream (java.io.OutputStream)38 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)27 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)24 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)23 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)19 QName (javax.xml.namespace.QName)18 IOException (java.io.IOException)17 XmlOptions (org.apache.xmlbeans.XmlOptions)17 InputStream (java.io.InputStream)11 Test (org.junit.Test)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 POIXMLException (org.apache.poi.POIXMLException)8 XmlException (org.apache.xmlbeans.XmlException)8 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)7 ArrayList (java.util.ArrayList)6 TikaException (org.apache.tika.exception.TikaException)6 URI (java.net.URI)5 SAXException (org.xml.sax.SAXException)5