1

I want to read excel files in java. I have some excel files with old format (excel 95) and others in new format (excel 2007). I am currently using poi but it is not able to read the excel files with older format. So what I need is a function that passes the filename which will return a boolean with value true if the format is old format (BIFF5) and false if the format is new (BIFF8). The need of this function is to allow me to use jxl in for older format and poi for newer format.

Here is the code I have:

    try
    {
      // create a new org.apache.poi.poifs.filesystem.Filesystem
      POIFSFileSystem poifs = new POIFSFileSystem(fin);
      w = new HSSFWorkbook(poifs);
    }
    catch (IOException e)
    {
      w = null;
      throw e;
    }
    catch (OutOfMemoryError e) // java.lang.OutOfMemoryError:
    {
      w = null;
      throw e;
    }
    catch (OldExcelFormatException e) // OldExcelFormatException
    {
      w = null;
      System.out.println("OldExcelFormatException");
      translateBIFF5();
    }

 private void translateBIFF5() throws IOException, CmpException
  {
    ArrayList<String> row = null;
    try
    {
      jxl_w = Workbook.getWorkbook(excelFile);
    }
    catch (BiffException e)
    {
      jxl_w = null;
      e.printStackTrace();
    }
    catch (IOException e)
    {
      jxl_w = null;
      throw e;
    }
    catch (OutOfMemoryError e) // java.lang.OutOfMemoryError:
    {
      jxl_w = null;
      throw e;
    }

    if (jxl_w != null)
    {
      try
      {
        for (currentSheet = 0; currentSheet < jxl_w.getNumberOfSheets(); currentSheet++)
        {
          jxl_sheet = jxl_w.getSheet(currentSheet);
      . . . . .
Wael
  • 1,533
  • 4
  • 20
  • 35
  • currently I am catching the OldExcelFormatException and then use jxl in this case. What I need to do is to detect that the format is old format and avoid catching exceptions in my code. – Wael Mar 13 '13 at 11:54

2 Answers2

1

I'd recommend trying Andy Khan's JExcel instead of POI. I don't think POI is particularly well designed or documented. I've had great luck with JExcel. Try it.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

One way is to call the Windows ASSOC and FTYPE commands, capture the output and parse it to determine the Office version installed.

C:\Users\me>assoc .xls
.xls=Excel.Sheet.8

C:\Users\me>ftype Excel.sheet.8
Excel.sheet.8="C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE" /e

Here a quick example :

import java.io.*;
public class ShowOfficeInstalled {
    public static void main(String argv[]) {
      try {
        Process p = Runtime.getRuntime().exec
          (new String [] { "cmd.exe", "/c", "assoc", ".xls"});
        BufferedReader input =
          new BufferedReader
            (new InputStreamReader(p.getInputStream()));
        String extensionType = input.readLine();
        input.close();
        // extract type
        if (extensionType == null) {
          System.out.println("no office installed ?");
          System.exit(1);
        }
        String fileType[] = extensionType.split("=");

        p = Runtime.getRuntime().exec
          (new String [] { "cmd.exe", "/c", "ftype", fileType[1]});
        input =
          new BufferedReader
            (new InputStreamReader(p.getInputStream()));
        String fileAssociation = input.readLine();
        // extract path
        String officePath = fileAssociation.split("=")[1];
        System.out.println(officePath);
      }
      catch (Exception err) {
        err.printStackTrace();
      }
    }
  }

or

You can search in the registry for the key:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths

This will probably require some work, as evidenced by this question:

read/write to Windows Registry using Java

Community
  • 1
  • 1
Shiva Komuravelly
  • 3,252
  • 1
  • 15
  • 16