Search in sources :

Example 6 with OPCPackage

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

the class XSSFSave method main.

public static void main(String[] args) throws Exception {
    for (int i = 0; i < args.length; i++) {
        OPCPackage pkg = OPCPackage.open(args[i]);
        XSSFWorkbook wb = new XSSFWorkbook(pkg);
        int sep = args[i].lastIndexOf('.');
        String outfile = args[i].substring(0, sep) + "-save.xls" + (wb.isMacroEnabled() ? "m" : "x");
        FileOutputStream out = new FileOutputStream(outfile);
        wb.write(out);
        out.close();
        pkg.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 7 with OPCPackage

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

the class SignatureInfo method getSignatureParts.

/**
     * @return a signature part for each signature document.
     * the parts can be validated independently.
     */
public Iterable<SignaturePart> getSignatureParts() {
    signatureConfig.init(true);
    return new Iterable<SignaturePart>() {

        public Iterator<SignaturePart> iterator() {
            return new Iterator<SignaturePart>() {

                OPCPackage pkg = signatureConfig.getOpcPackage();

                Iterator<PackageRelationship> sigOrigRels = pkg.getRelationshipsByType(PackageRelationshipTypes.DIGITAL_SIGNATURE_ORIGIN).iterator();

                Iterator<PackageRelationship> sigRels = null;

                PackagePart sigPart = null;

                public boolean hasNext() {
                    while (sigRels == null || !sigRels.hasNext()) {
                        if (!sigOrigRels.hasNext())
                            return false;
                        sigPart = pkg.getPart(sigOrigRels.next());
                        LOG.log(POILogger.DEBUG, "Digital Signature Origin part", sigPart);
                        try {
                            sigRels = sigPart.getRelationshipsByType(PackageRelationshipTypes.DIGITAL_SIGNATURE).iterator();
                        } catch (InvalidFormatException e) {
                            LOG.log(POILogger.WARN, "Reference to signature is invalid.", e);
                        }
                    }
                    return true;
                }

                public SignaturePart next() {
                    PackagePart sigRelPart = null;
                    do {
                        try {
                            if (!hasNext())
                                throw new NoSuchElementException();
                            sigRelPart = sigPart.getRelatedPart(sigRels.next());
                            LOG.log(POILogger.DEBUG, "XML Signature part", sigRelPart);
                        } catch (InvalidFormatException e) {
                            LOG.log(POILogger.WARN, "Reference to signature is invalid.", e);
                        }
                    } while (sigPart == null);
                    return new SignaturePart(sigRelPart);
                }

                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}
Also used : Iterator(java.util.Iterator) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) NoSuchElementException(java.util.NoSuchElementException)

Example 8 with OPCPackage

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

the class PackageHelper method clone.

/**
     * Clone the specified package.
     *
     * @param   pkg   the package to clone
     * @param   file  the destination file
     * @return  the cloned package
     */
public static OPCPackage clone(OPCPackage pkg, File file) throws OpenXML4JException, IOException {
    String path = file.getAbsolutePath();
    OPCPackage dest = OPCPackage.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;
        }
        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 OPCPackage.open(path);
}
Also used : OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 9 with OPCPackage

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

the class LoadPasswordProtectedXlsxStreaming method printSheetCount.

public static void printSheetCount(final InputStream inputStream) throws Exception {
    AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(inputStream);
    try {
        OPCPackage pkg = OPCPackage.open(source);
        try {
            XSSFReader reader = new XSSFReader(pkg);
            SheetIterator iter = (SheetIterator) reader.getSheetsData();
            int count = 0;
            while (iter.hasNext()) {
                iter.next();
                count++;
            }
            System.out.println("sheet count: " + count);
        } finally {
            IOUtils.closeQuietly(pkg);
        }
    } finally {
        IOUtils.closeQuietly(source);
    }
}
Also used : AesZipFileZipEntrySource(org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) SheetIterator(org.apache.poi.xssf.eventusermodel.XSSFReader.SheetIterator) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Example 10 with OPCPackage

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

the class FromHowTo method processAllSheets.

public void processAllSheets(String filename) throws Exception {
    OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
    try {
        XSSFReader r = new XSSFReader(pkg);
        SharedStringsTable sst = r.getSharedStringsTable();
        XMLReader parser = fetchSheetParser(sst);
        Iterator<InputStream> sheets = r.getSheetsData();
        while (sheets.hasNext()) {
            System.out.println("Processing new sheet:\n");
            InputStream sheet = sheets.next();
            InputSource sheetSource = new InputSource(sheet);
            parser.parse(sheetSource);
            sheet.close();
            System.out.println("");
        }
    } finally {
        pkg.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) InputStream(java.io.InputStream) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XMLReader(org.xml.sax.XMLReader) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Aggregations

OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)116 Test (org.junit.Test)54 InputStream (java.io.InputStream)29 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)21 File (java.io.File)19 IOException (java.io.IOException)17 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)13 SignatureConfig (org.apache.poi.poifs.crypt.dsig.SignatureConfig)10 SignatureInfo (org.apache.poi.poifs.crypt.dsig.SignatureInfo)10 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)10 OutputStream (java.io.OutputStream)9 ArrayList (java.util.ArrayList)9 FileOutputStream (java.io.FileOutputStream)8 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)8 XmlException (org.apache.xmlbeans.XmlException)8 FileInputStream (java.io.FileInputStream)7