Example usage for org.apache.poi.poifs.filesystem POIFSFileSystem createDocumentInputStream

List of usage examples for org.apache.poi.poifs.filesystem POIFSFileSystem createDocumentInputStream

Introduction

In this page you can find the example usage for org.apache.poi.poifs.filesystem POIFSFileSystem createDocumentInputStream.

Prototype

public DocumentInputStream createDocumentInputStream(final String documentName) throws IOException 

Source Link

Document

open a document in the root entry's list of entries

Usage

From source file:poi.hssf.usermodel.examples.EventExample.java

License:Apache License

/**
 * Read an excel file and spit out what we find.
 *
 * @param args      Expect one argument that is the file to read.
 * @throws java.io.IOException  When there is an error processing the file.
 *//*  ww w  .  j a va2s  .c  o  m*/
public static void main(String[] args) throws IOException {
    // create a new file input stream with the input file specified
    // at the command line
    FileInputStream fin = new FileInputStream(args[0]);
    // create a new org.apache.poi.poifs.filesystem.Filesystem
    POIFSFileSystem poifs = new POIFSFileSystem(fin);
    // get the Workbook (excel part) stream in a InputStream
    InputStream din = poifs.createDocumentInputStream("Workbook");
    // construct out HSSFRequest object
    HSSFRequest req = new HSSFRequest();
    // lazy listen for ALL records with the listener shown above
    req.addListenerForAllRecords(new EventExample());
    // create our event factory
    HSSFEventFactory factory = new HSSFEventFactory();
    // process our events based on the document input stream
    factory.processEvents(req, din);
    // once all the events are processed close our file input stream
    fin.close();
    // and our document input stream (don't want to leak these!)
    din.close();
    System.out.println("done.");
}

From source file:util.XLSReader.java

License:Open Source License

/**
 * Read an excel file and return found cell elements.
 * //w  w w.j a v  a 2s. c o m
 * @param aBufferedInputStream
 * @throws IOException
 * @return List<List<String>>
 */
public List<List<String>> read(final BufferedInputStream ins) throws IOException {

    InputStream din = null;

    try {

        // create a new org.apache.poi.poifs.filesystem.Filesystem
        final POIFSFileSystem poifs = new POIFSFileSystem(ins);
        // get the Workbook (excel part) stream in a InputStream
        din = poifs.createDocumentInputStream("Workbook");
        // construct out HSSFRequest object
        final HSSFRequest req = new HSSFRequest();
        // lazy listen for ALL records with the listener shown above
        req.addListenerForAllRecords(this);
        // create our event factory
        final HSSFEventFactory factory = new HSSFEventFactory();
        // process our events based on the document input stream
        factory.processEvents(req, din);

        // add last row into result
        if (!rowcontent.isEmpty()) {
            result.add(rowcontent);
        }

    } finally {
        // and our document input stream (don't want to leak these!)
        try {
            din.close();
        } catch (final Exception e) {
            LOG.error(e.toString());
        }
    }

    return getResult();

}