Search in sources :

Example 6 with PackagePart

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

the class XSLFSheet method importPart.

/**
     * Import a package part into this sheet.
     */
PackagePart importPart(PackageRelationship srcRel, PackagePart srcPafrt) {
    PackagePart destPP = getPackagePart();
    PackagePartName srcPPName = srcPafrt.getPartName();
    OPCPackage pkg = destPP.getPackage();
    if (pkg.containPart(srcPPName)) {
        // already exists
        return pkg.getPart(srcPPName);
    }
    destPP.addRelationship(srcPPName, TargetMode.INTERNAL, srcRel.getRelationshipType());
    PackagePart part = pkg.createPart(srcPPName, srcPafrt.getContentType());
    try {
        OutputStream out = part.getOutputStream();
        InputStream is = srcPafrt.getInputStream();
        IOUtils.copy(is, out);
        is.close();
        out.close();
    } catch (IOException e) {
        throw new POIXMLException(e);
    }
    return part;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 7 with PackagePart

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

the class XSLFPictureShape method getPictureLink.

/**
     * For an external linked picture, return the last-seen
     *  path to the picture.
     * For an internal picture, returns null.
     */
public URI getPictureLink() {
    if (getBlipId() != null) {
        // Internal picture, nothing to return
        return null;
    }
    String rId = getBlipLink();
    if (rId == null) {
        // No link recorded, nothing we can do
        return null;
    }
    PackagePart p = getSheet().getPackagePart();
    PackageRelationship rel = p.getRelationship(rId);
    if (rel != null) {
        return rel.getTargetURI();
    }
    return null;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 8 with PackagePart

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

the class XSSFReader method getStylesTable.

/**
     * Opens up the Styles Table, parses it, and
     *  returns a handy object for working with cell styles
     */
public StylesTable getStylesTable() throws IOException, InvalidFormatException {
    ArrayList<PackagePart> parts = pkg.getPartsByContentType(XSSFRelation.STYLES.getContentType());
    if (parts.size() == 0)
        return null;
    // Create the Styles Table, and associate the Themes if present
    StylesTable styles = new StylesTable(parts.get(0));
    parts = pkg.getPartsByContentType(XSSFRelation.THEME.getContentType());
    if (parts.size() != 0) {
        styles.setTheme(new ThemesTable(parts.get(0)));
    }
    return styles;
}
Also used : ThemesTable(org.apache.poi.xssf.model.ThemesTable) StylesTable(org.apache.poi.xssf.model.StylesTable) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 9 with PackagePart

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

the class XSSFReader method getSheet.

/**
     * Returns an InputStream to read the contents of the
     *  specified Sheet.
     * @param relId The relationId of the sheet, from a r:id on the workbook
     */
public InputStream getSheet(String relId) throws IOException, InvalidFormatException {
    PackageRelationship rel = workbookPart.getRelationship(relId);
    if (rel == null) {
        throw new IllegalArgumentException("No Sheet found with r:id " + relId);
    }
    PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
    PackagePart sheet = pkg.getPart(relName);
    if (sheet == null) {
        throw new IllegalArgumentException("No data found for Sheet with r:id " + relId);
    }
    return sheet.getInputStream();
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 10 with PackagePart

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

the class StylesTable method commit.

@Override
protected void commit() throws IOException {
    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    writeTo(out);
    out.close();
}
Also used : OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

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