Thursday, July 19, 2012

Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given InputStream

I waited enough for Sandip to write this blog since it was his find :).

Here is the problem we had, we have an application where user can upload the excels and our application process them. Our users were uploading 2007 formatted excel file in .xls format or vices-verse. Microsoft excel 2007 is backward compatible but our POI libraries sometime gets confuse. DOM parser is smart enough to handle but when it comes to SAX we need to know the exact format of the file loaded. i.e. just checking the file extension might not be enough. So we have to look into input stream to find the exact loaded excel format of the file.

As usual I looked over the internet and could not find anything post how to do it. Since Sandip is lot more savvy with Apache POI he started looking into the Apace POI source code and found WorkbookFactory class (Factory for creating the appropriate kind of Workbook) has a help method which takes the inputStream and based on that creates an appropriate object. Look at the code below.

============================
/**
 * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
 *  the given InputStream.
 * Your input stream MUST either support mark/reset, or
 *  be wrapped as a {@link PushbackInputStream}!
 */
public static Workbook create(InputStream inp) throws IOException, InvalidFormatException {
    // If clearly doesn't do mark/reset, wrap up
    if(! inp.markSupported()) {
        inp = new PushbackInputStream(inp, 8);
    }
   
    if(POIFSFileSystem.hasPOIFSHeader(inp)) {
        return new HSSFWorkbook(inp);
    }
    if(POIXMLDocument.hasOOXMLHeader(inp)) {
        return new XSSFWorkbook(OPCPackage.open(inp));
    }
    throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}

============================

Good find Sandip
Manisha

No comments:

Post a Comment