With great help from Yury I found the following principal solution which
might help others to solve similar questions.

(My original test input converted the embedded Excel table somehow to a Word
table (?))

---
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.poi.hssf.record.RecordFormatException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class POIFSExtract {

    public static void main(String[] args) throws Exception {
        if (args == null || args.length < 1)
            throw new Exception("\n->no input, no output<-");

        final String fileName = args[0];
        POIFSFileSystem fs = new POIFSFileSystem(new
FileInputStream(fileName));

        POIFSReader reader = new POIFSReader();
        reader.registerListener(new MyPOIFSReaderListener(fs));
        reader.read(new FileInputStream(fileName));
    }

    static class MyPOIFSReaderListener implements POIFSReaderListener {

        private POIFSFileSystem fs;

        public MyPOIFSReaderListener(POIFSFileSystem fs) {
            this.fs = fs;
        }

        public void processPOIFSReaderEvent(POIFSReaderEvent event) {

            String name = event.getName();
            POIFSDocumentPath path = event.getPath();
            System.out.println("got '" + name + "' event for path '" + path
+ "'.");

            if (name.endsWith("Workbook") || name.endsWith("WORKBOOK")) {
                try {
                    DirectoryNode dir = resolveDir(fs,
event.getPath().toString()); // converting it to DirectoryNode
                    System.out.println("  trying DirectoryNode '" +
dir.getName() + "'");
                    HSSFWorkbook wb = new HSSFWorkbook(dir, fs, true); //
Invoke HSSFWorkbook constructor passing this directory to it
                    System.out.println("  !!! success: workbook with " +
wb.getNumberOfSheets() + " sheets.");
                }
                catch (RecordFormatException e) {
                    // MS Graph charts are stored in "Workbook" stream too
                    System.out.println("  skipping embedded MS Graph
object!");
                }
                catch (Exception e) {
                    System.out.println("  " + e.getMessage());
                    throw new RuntimeException(e.getMessage());
                }
            }
        }

        static DirectoryNode resolveDir(POIFSFileSystem filesystem, String
path) throws FileNotFoundException {
            DirectoryNode dir = filesystem.getRoot();

            for (String token : path.split("\\" + File.separator)) {
                if (!token.equals("") && !token.equals(File.separator))
                    dir = (DirectoryNode) dir.getEntry(token);
            }

            return dir;
        }
    }
}
---

Cheers,
Axel.

Reply via email to