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.

EmbeddedObjects.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.hssf.usermodel;
  16. import java.io.Closeable;
  17. import java.io.FileInputStream;
  18. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  19. import org.apache.poi.hssf.usermodel.HSSFObjectData;
  20. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  21. import org.apache.poi.hwpf.HWPFDocument;
  22. import org.apache.poi.poifs.filesystem.DirectoryNode;
  23. import org.apache.poi.poifs.filesystem.Entry;
  24. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  25. /**
  26. * Demonstrates how you can extract embedded data from a .xls file
  27. */
  28. @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
  29. public final class EmbeddedObjects {
  30. private EmbeddedObjects() {}
  31. @SuppressWarnings("unused")
  32. public static void main(String[] args) throws Exception {
  33. try (
  34. FileInputStream fis = new FileInputStream(args[0]);
  35. POIFSFileSystem fs = new POIFSFileSystem(fis);
  36. HSSFWorkbook workbook = new HSSFWorkbook(fs)
  37. ) {
  38. for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
  39. //the OLE2 Class Name of the object
  40. String oleName = obj.getOLE2ClassName();
  41. DirectoryNode dn = (obj.hasDirectoryEntry()) ? (DirectoryNode) obj.getDirectory() : null;
  42. Closeable document = null;
  43. switch (oleName) {
  44. case "Worksheet":
  45. document = new HSSFWorkbook(dn, fs, false);
  46. break;
  47. case "Document":
  48. document = new HWPFDocument(dn);
  49. break;
  50. case "Presentation":
  51. document = new HSLFSlideShow(dn);
  52. break;
  53. default:
  54. if (dn != null) {
  55. // The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is
  56. for (Entry entry : dn) {
  57. String name = entry.getName();
  58. }
  59. } else {
  60. // There is no DirectoryEntry
  61. // Recover the object's data from the HSSFObjectData instance.
  62. byte[] objectData = obj.getObjectData();
  63. }
  64. break;
  65. }
  66. if (document != null) {
  67. document.close();
  68. }
  69. }
  70. }
  71. }
  72. }