Search in sources :

Example 41 with PackagePart

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

the class DataExtraction method main.

public static void main(String[] args) throws IOException, OpenXML4JException {
    PrintStream out = System.out;
    if (args.length == 0) {
        out.println("Input file is required");
        return;
    }
    FileInputStream is = new FileInputStream(args[0]);
    XMLSlideShow ppt = new XMLSlideShow(is);
    is.close();
    // Get the document's embedded files.
    for (PackagePart p : ppt.getAllEmbedds()) {
        String type = p.getContentType();
        // typically file name
        String name = p.getPartName().getName();
        out.println("Embedded file (" + type + "): " + name);
        InputStream pIs = p.getInputStream();
        // make sense of the part data
        pIs.close();
    }
    // Get the document's embedded files.
    for (XSLFPictureData data : ppt.getPictureData()) {
        String type = data.getContentType();
        String name = data.getFileName();
        out.println("Picture (" + type + "): " + name);
        InputStream pIs = data.getInputStream();
        // make sense of the image data
        pIs.close();
    }
    // size of the canvas in points
    Dimension pageSize = ppt.getPageSize();
    out.println("Pagesize: " + pageSize);
    for (XSLFSlide slide : ppt.getSlides()) {
        for (XSLFShape shape : slide) {
            if (shape instanceof XSLFTextShape) {
                XSLFTextShape txShape = (XSLFTextShape) shape;
                out.println(txShape.getText());
            } else if (shape instanceof XSLFPictureShape) {
                XSLFPictureShape pShape = (XSLFPictureShape) shape;
                XSLFPictureData pData = pShape.getPictureData();
                out.println(pData.getFileName());
            } else {
                out.println("Process me: " + shape.getClass());
            }
        }
    }
    ppt.close();
}
Also used : PrintStream(java.io.PrintStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Dimension(java.awt.Dimension) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) FileInputStream(java.io.FileInputStream)

Example 42 with PackagePart

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

the class XSLFChart method commit.

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

Example 43 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)

Example 44 with PackagePart

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

the class TestXSSFWorkbook method saveLoadNew.

/**
     * Tests that we can save, and then re-load a new document
     */
@Test
public void saveLoadNew() throws IOException, InvalidFormatException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    //check that the default date system is set to 1900
    CTWorkbookPr pr = wb1.getCTWorkbook().getWorkbookPr();
    assertNotNull(pr);
    assertTrue(pr.isSetDate1904());
    assertFalse("XSSF must use the 1900 date system", pr.getDate1904());
    Sheet sheet1 = wb1.createSheet("sheet1");
    Sheet sheet2 = wb1.createSheet("sheet2");
    wb1.createSheet("sheet3");
    RichTextString rts = wb1.getCreationHelper().createRichTextString("hello world");
    sheet1.createRow(0).createCell((short) 0).setCellValue(1.2);
    sheet1.createRow(1).createCell((short) 0).setCellValue(rts);
    sheet2.createRow(0);
    assertEquals(0, wb1.getSheetAt(0).getFirstRowNum());
    assertEquals(1, wb1.getSheetAt(0).getLastRowNum());
    assertEquals(0, wb1.getSheetAt(1).getFirstRowNum());
    assertEquals(0, wb1.getSheetAt(1).getLastRowNum());
    assertEquals(0, wb1.getSheetAt(2).getFirstRowNum());
    assertEquals(0, wb1.getSheetAt(2).getLastRowNum());
    File file = TempFile.createTempFile("poi-", ".xlsx");
    OutputStream out = new FileOutputStream(file);
    wb1.write(out);
    out.close();
    // Check the package contains what we'd expect it to
    OPCPackage pkg = OPCPackage.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());
    wb1.close();
    // Load back the XSSFWorkbook
    @SuppressWarnings("resource") XSSFWorkbook wb2 = new XSSFWorkbook(pkg);
    assertEquals(3, wb2.getNumberOfSheets());
    assertNotNull(wb2.getSheetAt(0));
    assertNotNull(wb2.getSheetAt(1));
    assertNotNull(wb2.getSheetAt(2));
    assertNotNull(wb2.getSharedStringSource());
    assertNotNull(wb2.getStylesSource());
    assertEquals(0, wb2.getSheetAt(0).getFirstRowNum());
    assertEquals(1, wb2.getSheetAt(0).getLastRowNum());
    assertEquals(0, wb2.getSheetAt(1).getFirstRowNum());
    assertEquals(0, wb2.getSheetAt(1).getLastRowNum());
    assertEquals(0, wb2.getSheetAt(2).getFirstRowNum());
    assertEquals(0, wb2.getSheetAt(2).getLastRowNum());
    sheet1 = wb2.getSheetAt(0);
    assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
    assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
    pkg.close();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) CTWorkbookPr(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr) RichTextString(org.apache.poi.ss.usermodel.RichTextString) MemoryPackagePart(org.apache.poi.openxml4j.opc.internal.MemoryPackagePart) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Sheet(org.apache.poi.ss.usermodel.Sheet) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 45 with PackagePart

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

the class XSSFWorkbook method setVBAProject.

/**
     * Adds a vbaProject.bin file to the workbook.  This will change the workbook
     * type if necessary.
     *
     * @throws IOException
     */
public void setVBAProject(InputStream vbaProjectStream) throws IOException {
    if (!isMacroEnabled()) {
        setWorkbookType(XSSFWorkbookType.XLSM);
    }
    PackagePartName ppName;
    try {
        ppName = PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName());
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
    OPCPackage opc = getPackage();
    OutputStream outputStream;
    if (!opc.containPart(ppName)) {
        POIXMLDocumentPart relationship = createRelationship(XSSFRelation.VBA_MACROS, XSSFFactory.getInstance());
        outputStream = relationship.getPackagePart().getOutputStream();
    } else {
        PackagePart part = opc.getPart(ppName);
        outputStream = part.getOutputStream();
    }
    try {
        IOUtils.copy(vbaProjectStream, outputStream);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

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