Determine version of Microsoft Office with java

I wrote a program that creates a set of data that is displayed in an Excel spreadsheet. I originally used the jexcel library to write data to a file, but I would like to update the program so that it can check whether to create a ".xls" or ".xlsx" file and then write to the appropriate document type. Apache POI seems to be the best option in terms of writing the ".xlsx" file, but any ideas on determining the correct file type?

I could just select the user when naming the file, but this seems like extra work for the user, and I assume that there are users who do not know what type of file they want.

Any ideas?

In addition, I assume that the OS is windows, and the user has an excel version, in other cases I just select the default file type.

+4
java file-type excel ms-office version
May 31 '11 at 21:51
source share
3 answers

One way is to invoke the Windows ASSOC and FTYPE commands, capture the output, and analyze it to determine the installed version of 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 

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

You might want to add additional error checking, and the parsing to extract the version of Office from the return path is left as an exercise; -)

+6
May 31 '11 at 23:29
source share

You can search the registry for the key:

HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows \ CurrentVersion \ Application Paths

This is likely to require some work, as evidenced by this question:

read / write to the windows registry using java

+1
May 31 '11 at 21:57
source share

If you want to dive into the registry (e.g. jregistrykey ), the translated version of this PowerShell script should do what you want.

0
May 31 '11 at 21:56
source share



All Articles