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.

LoadEmbedded.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.examples.ss;
  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.hslf.usermodel.HSLFSlideShow;
  21. import org.apache.poi.hssf.usermodel.HSSFObjectData;
  22. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  23. import org.apache.poi.hwpf.HWPFDocument;
  24. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  25. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  26. import org.apache.poi.openxml4j.opc.PackagePart;
  27. import org.apache.poi.poifs.filesystem.DirectoryNode;
  28. import org.apache.poi.poifs.filesystem.Entry;
  29. import org.apache.poi.sl.usermodel.SlideShow;
  30. import org.apache.poi.ss.usermodel.Workbook;
  31. import org.apache.poi.ss.usermodel.WorkbookFactory;
  32. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  33. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  34. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  35. import org.apache.xmlbeans.XmlException;
  36. /**
  37. * Loads embedded resources from Workbooks. Code taken from the website:
  38. * https://poi.apache.org/spreadsheet/quick-guide.html#Embedded
  39. */
  40. @SuppressWarnings({"java:S106","java:S4823"})
  41. public final class LoadEmbedded {
  42. private LoadEmbedded() {}
  43. public static void main(String[] args) throws IOException, EncryptedDocumentException, OpenXML4JException, XmlException {
  44. Workbook wb = WorkbookFactory.create(new File(args[0]));
  45. loadEmbedded(wb);
  46. }
  47. public static void loadEmbedded(Workbook wb) throws IOException, InvalidFormatException, OpenXML4JException, XmlException {
  48. if (wb instanceof HSSFWorkbook) {
  49. loadEmbedded((HSSFWorkbook)wb);
  50. }
  51. else if (wb instanceof XSSFWorkbook) {
  52. loadEmbedded((XSSFWorkbook)wb);
  53. }
  54. else {
  55. throw new IllegalArgumentException(wb.getClass().getName());
  56. }
  57. }
  58. public static void loadEmbedded(HSSFWorkbook workbook) throws IOException {
  59. for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
  60. //the OLE2 Class Name of the object
  61. String oleName = obj.getOLE2ClassName();
  62. if (oleName.equals("Worksheet")) {
  63. DirectoryNode dn = (DirectoryNode) obj.getDirectory();
  64. HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(dn, false);
  65. embeddedWorkbook.close();
  66. } else if (oleName.equals("Document")) {
  67. DirectoryNode dn = (DirectoryNode) obj.getDirectory();
  68. HWPFDocument embeddedWordDocument = new HWPFDocument(dn);
  69. embeddedWordDocument.close();
  70. } else if (oleName.equals("Presentation")) {
  71. DirectoryNode dn = (DirectoryNode) obj.getDirectory();
  72. SlideShow<?,?> embeddedSlieShow = new HSLFSlideShow(dn);
  73. embeddedSlieShow.close();
  74. } else {
  75. if(obj.hasDirectoryEntry()){
  76. // The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is
  77. DirectoryNode dn = (DirectoryNode) obj.getDirectory();
  78. for (Entry entry : dn) {
  79. //System.out.println(oleName + "." + entry.getName());
  80. }
  81. } else {
  82. // There is no DirectoryEntry
  83. // Recover the object's data from the HSSFObjectData instance.
  84. byte[] objectData = obj.getObjectData();
  85. }
  86. }
  87. }
  88. }
  89. public static void loadEmbedded(XSSFWorkbook workbook) throws IOException, InvalidFormatException, OpenXML4JException, XmlException {
  90. for (PackagePart pPart : workbook.getAllEmbeddedParts()) {
  91. String contentType = pPart.getContentType();
  92. if (contentType.equals("application/vnd.ms-excel")) {
  93. // Excel Workbook - either binary or OpenXML
  94. HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
  95. embeddedWorkbook.close();
  96. } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
  97. // Excel Workbook - OpenXML file format
  98. XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream());
  99. embeddedWorkbook.close();
  100. } else if (contentType.equals("application/msword")) {
  101. // Word Document - binary (OLE2CDF) file format
  102. HWPFDocument document = new HWPFDocument(pPart.getInputStream());
  103. document.close();
  104. } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
  105. // Word Document - OpenXML file format
  106. XWPFDocument document = new XWPFDocument(pPart.getInputStream());
  107. document.close();
  108. } else if (contentType.equals("application/vnd.ms-powerpoint")) {
  109. // PowerPoint Document - binary file format
  110. HSLFSlideShow slideShow = new HSLFSlideShow(pPart.getInputStream());
  111. slideShow.close();
  112. } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
  113. // PowerPoint Document - OpenXML file format
  114. XMLSlideShow slideShow = new XMLSlideShow(pPart.getInputStream());
  115. slideShow.close();
  116. } else {
  117. // Any other type of embedded object.
  118. System.out.println("Unknown Embedded Document: " + contentType);
  119. InputStream inputStream = pPart.getInputStream();
  120. inputStream.close();
  121. }
  122. }
  123. }
  124. }