2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
20 package org.apache.poi.examples.xssf.eventusermodel;
22 import java.io.InputStream;
24 import org.apache.poi.examples.xssf.usermodel.LoadPasswordProtectedXlsx;
25 import org.apache.poi.openxml4j.opc.OPCPackage;
26 import org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource;
27 import org.apache.poi.xssf.eventusermodel.XSSFReader;
28 import org.apache.poi.xssf.eventusermodel.XSSFReader.SheetIterator;
31 * An example that loads a password protected workbook and counts the sheets.
32 * The example highlights how to do this in streaming way.
34 * <li>The example demonstrates that all temp files are removed.
35 * <li>{@code AesZipFileZipEntrySource} is used to ensure that temp files are encrypted.
38 @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
39 public final class LoadPasswordProtectedXlsxStreaming {
41 private LoadPasswordProtectedXlsxStreaming() {}
43 public static void main(String[] args) throws Exception {
44 LoadPasswordProtectedXlsx.execute(args, LoadPasswordProtectedXlsxStreaming::printSheetCount);
47 private static void printSheetCount(final InputStream inputStream) throws Exception {
48 try (AesZipFileZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(inputStream);
49 OPCPackage pkg = OPCPackage.open(source)) {
50 XSSFReader reader = new XSSFReader(pkg);
51 SheetIterator iter = (SheetIterator)reader.getSheetsData();
53 while(iter.hasNext()) {
57 System.out.println("sheet count: " + count);