You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSSFWorkbookFactory.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import org.apache.poi.EncryptedDocumentException;
  20. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  21. import org.apache.poi.openxml4j.opc.OPCPackage;
  22. import org.apache.poi.openxml4j.opc.PackageAccess;
  23. import org.apache.poi.poifs.filesystem.DirectoryNode;
  24. import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
  25. import org.apache.poi.poifs.filesystem.FileMagic;
  26. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  27. import org.apache.poi.ss.usermodel.Workbook;
  28. import org.apache.poi.ss.usermodel.WorkbookProvider;
  29. import org.apache.poi.util.Internal;
  30. @Internal
  31. public class XSSFWorkbookFactory implements WorkbookProvider {
  32. @Override
  33. public boolean accepts(FileMagic fm) {
  34. return fm == FileMagic.OOXML;
  35. }
  36. /**
  37. * Create a new empty Workbook
  38. *
  39. * @return The created workbook
  40. */
  41. @Override
  42. public XSSFWorkbook create() {
  43. return new XSSFWorkbook();
  44. }
  45. @Override
  46. public XSSFWorkbook create(DirectoryNode root, String password) throws IOException {
  47. try (InputStream stream = DocumentFactoryHelper.getDecryptedStream(root, password)) {
  48. return create(stream);
  49. // don't close root.getFileSystem() otherwise the container filesystem becomes invalid
  50. }
  51. }
  52. @Override
  53. public Workbook create(InputStream inp, String password) throws IOException {
  54. InputStream bufInp = FileMagic.prepareToCheckMagic(inp);
  55. FileMagic fm = FileMagic.valueOf(bufInp);
  56. if (fm == FileMagic.OLE2) {
  57. try (POIFSFileSystem poifs = new POIFSFileSystem(bufInp);
  58. InputStream stream = DocumentFactoryHelper.getDecryptedStream(poifs.getRoot(), password)) {
  59. return create(stream);
  60. }
  61. }
  62. if (fm == FileMagic.OOXML) {
  63. return create(bufInp);
  64. }
  65. return null;
  66. }
  67. /**
  68. * Creates a XSSFWorkbook from the given InputStream
  69. *
  70. * <p>Note that in order to properly release resources the
  71. * Workbook should be closed after use.</p>
  72. *
  73. * @param stream The {@link InputStream} to read data from.
  74. *
  75. * @return The created Workbook
  76. *
  77. * @throws IOException if an error occurs while reading the data
  78. */
  79. @SuppressWarnings("resource")
  80. @Override
  81. public XSSFWorkbook create(InputStream stream) throws IOException {
  82. try {
  83. OPCPackage pkg = OPCPackage.open(stream);
  84. return createWorkbook(pkg);
  85. } catch (InvalidFormatException e) {
  86. throw new IOException(e);
  87. }
  88. }
  89. /**
  90. * Creates a XSSFWorkbook from the given OOXML Package
  91. *
  92. * <p>Note that in order to properly release resources the
  93. * Workbook should be closed after use.</p>
  94. *
  95. * @param pkg The {@link OPCPackage} opened for reading data.
  96. *
  97. * @return The created Workbook
  98. *
  99. * @throws IOException if an error occurs while reading the data
  100. */
  101. public static XSSFWorkbook createWorkbook(OPCPackage pkg) throws IOException {
  102. try {
  103. return new XSSFWorkbook(pkg);
  104. } catch (RuntimeException ioe) {
  105. // ensure that file handles are closed (use revert() to not re-write the file)
  106. pkg.revert();
  107. //pkg.close();
  108. // rethrow exception
  109. throw ioe;
  110. }
  111. }
  112. /**
  113. * Creates the XSSFWorkbook from the given File, which must exist and be readable.
  114. * <p>Note that in order to properly release resources the Workbook should be closed after use.
  115. *
  116. * @param file The file to read data from.
  117. * @param readOnly If the Workbook should be opened in read-only mode to avoid writing back
  118. * changes when the document is closed.
  119. *
  120. * @return The created Workbook
  121. *
  122. * @throws IOException if an error occurs while reading the data
  123. * @throws EncryptedDocumentException If the wrong password is given for a protected file
  124. */
  125. @Override
  126. @SuppressWarnings("resource")
  127. public XSSFWorkbook create(File file, String password, boolean readOnly) throws IOException {
  128. FileMagic fm = FileMagic.valueOf(file);
  129. if (fm == FileMagic.OLE2) {
  130. try (POIFSFileSystem poifs = new POIFSFileSystem(file, true);
  131. InputStream stream = DocumentFactoryHelper.getDecryptedStream(poifs.getRoot(), password)) {
  132. return create(stream);
  133. }
  134. }
  135. try {
  136. OPCPackage pkg = OPCPackage.open(file, readOnly ? PackageAccess.READ : PackageAccess.READ_WRITE);
  137. return createWorkbook(pkg);
  138. } catch (InvalidFormatException e) {
  139. throw new IOException(e);
  140. }
  141. }
  142. }