一种方法是调用Windows ASSOC和FTYPE命令,捕获输出并进行解析以确定已安装的Office版本。
C:Usersme>assoc .xls.xls=Excel.Sheet.8C:Usersme>ftype Excel.sheet.8Excel.sheet.8="C:Program Files (x86)Microsoft OfficeOffice12EXCEL.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(); } } }您可能想要添加更多错误检查,并且将练习从返回的路径中提取Office版本的解析作为练习;-)



