title: What is the best way to determine if HSSF or XSSF is needed in Apache POI?
body: <p>I implement an upload service for Microsoft Excel files and can not be sure what kind of file is uploaded. Sometimes users upload files with xls extension that contain xlsx content.
  To parse the files I use Apache Poi and currently I try at first to parse the uploaded file as HSSFWorkbook and if I catch an exception then I try to create a XSSFWorkbook.</p>
  
  <p>Is there any smarter way to detect what version is needed?</p>
  
  <pre><code>/** should parse the uploaded file */
  private void handleUpload(File file) {
    Workbook wb = tryToHandleHSSF(file);
    if (wb==null)
      wb = tryToHandleXSSF(file);
    if (wb!=null) {
      // ... do the parsing stuff
    }
  }
  
  /** helper for HSSF */
  private Workbook tryToHandleHSSF(File file) {
    try {
      return new HSSFWorkbook(new FileInputStream(file));
    }
    catch(Exception e) {
      return null;
    }
  }
  
  /** helper for XSSF */
  private Workbook tryToHandleXSSF(File file) {
    Workbook workbook;
    try {
      InputStream fin = new FileInputStream(file);
      BufferedInputStream in = new BufferedInputStream(fin);
      try {
          if (POIFSFileSystem.hasPOIFSHeader(in)) {
              // if the file is encrypted
              POIFSFileSystem fs = new POIFSFileSystem(in);
              EncryptionInfo info = new EncryptionInfo(fs);
              Decryptor d = Decryptor.getInstance(info);
              d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
              workbook = new org.apache.poi.xssf.usermodel.XSSFWorkbook(d.getDataStream(fs));
          }
          else
              return new org.apache.poi.xssf.usermodel.XSSFWorkbook(in);
      }
      finally {
          in.close();
      }
    }
    catch(Exception e) {
      return null;
    }
  }
  </code></pre>