如何知道java中的excel文件版本?

如何知道java中的excel文件版本?,java,excel,apache-poi,jxl,Java,Excel,Apache Poi,Jxl,我想用java阅读excel文件。我有一些旧格式的excel文件(excel 95),还有一些新格式的excel文件(excel 2007)。我目前正在使用poi,但它无法读取旧格式的excel文件。所以我需要一个传递文件名的函数,如果格式是旧格式(BIFF5),它将返回一个布尔值true,如果格式是新格式(BIFF8),它将返回false。这个函数的需要是允许我在旧格式中使用jxl,在新格式中使用poi 以下是我的代码: try { // create a new

我想用java阅读excel文件。我有一些旧格式的excel文件(excel 95),还有一些新格式的excel文件(excel 2007)。我目前正在使用poi,但它无法读取旧格式的excel文件。所以我需要一个传递文件名的函数,如果格式是旧格式(BIFF5),它将返回一个布尔值true,如果格式是新格式(BIFF8),它将返回false。这个函数的需要是允许我在旧格式中使用jxl,在新格式中使用poi

以下是我的代码:

    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);
      . . . . .
试试看
{
//创建一个新的org.apache.poi.poifs.filesystem.filesystem
POIFSFileSystem poifs=新的POIFSFileSystem(fin);
w=新的HSSF工作手册(poifs);
}
捕获(IOE异常)
{
w=零;
投掷e;
}
catch(OutOfMemoryError)//java.lang.OutOfMemoryError:
{
w=零;
投掷e;
}
catch(OldExcelFormatException)//OldExcelFormatException
{
w=零;
System.out.println(“OldExcelFormatException”);
translateBIFF5();
}
私有void translateBIFF5()引发IOException、CMPEException
{
ArrayList行=null;
尝试
{
jxl_w=Workbook.getWorkbook(excelFile);
}
捕获(双飞例外e)
{
jxl_w=null;
e、 printStackTrace();
}
捕获(IOE异常)
{
jxl_w=null;
投掷e;
}
catch(OutOfMemoryError)//java.lang.OutOfMemoryError:
{
jxl_w=null;
投掷e;
}
如果(jxl_w!=null)
{
尝试
{
对于(currentSheet=0;currentSheet
我建议尝试Andy Khan的JExcel而不是POI。我认为POI的设计或文档不是特别好。我很幸运使用了JExcel。试试看。

一种方法是调用Windows ASSOC和FTYPE命令,捕获输出并对其进行分析,以确定安装的Office版本

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
下面是一个简单的例子:

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();
      }
    }
  }

您可以在注册表中搜索该项:

HKEY\ U本地\计算机\软件\ Microsoft\Windows\CurrentVersion\App路径

这可能需要一些工作,如以下问题所示:


目前我正在捕获旧ExcelFormatException,然后在本例中使用jxl。我需要做的是检测该格式是否为旧格式,并避免捕获代码中的异常。