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();
}
}
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();
}
};
}
};
}
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);
}
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);
}
}
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();
}
}
Aggregations