用java确定微软Office的版本 [英] determine version of microsoft office with java

查看:198
本文介绍了用java确定微软Office的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序,创建一组输出到excel电子表格的数据。我最初使用jexcel库将数据写入文件,但是我想更新程序,以便它可以检查是否应该创建一个.xls或.xlsx文件,然后写入适当的文件类型。 Apache POI似乎是写入.xlsx文件的最佳选择,但是有关确定正确的文件类型的任何想法?



我可以让用户在命名文件时进行选择,但这对用户来说似乎是额外的工作,我假设有不知道的用户他们想要什么文件类型。



任何想法?



另外,我假设操作系统是Windows和用户有一些版本的excel,在其他情况下,我只需要选择一个默认文件类型。

解决方案

是调用Windows ASSOC和FTYPE命令,捕获输出并解析它以确定安装的Office版本。

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

C:\Users\me> ftype Excel.sheet.8
Excel。 sheet.8 =C:\程序文件(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
[] {cmd.exe,/ c,assoc,.xls});
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String extensionType = input.readLine();
input.close();
//提取类型
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();
//提取路径
String officePath = fileAssociation.split(=)[1];
System.out.println(officePath);
}
catch(异常错误){
err.printStackTrace();
}
}
}

您可能想添加更多错误检查和从返回的路径中提取Office版本的解析作为一个练习; - )


I wrote a program that creates a set of data that is outputted to an excel spreadsheet. I was originally using the jexcel library to write the data to the file, but I'd like to update the program so that it can check and see whether is should create a ".xls" or ".xlsx" file, then write to the appropriate document type. Apache POI seems to be the best option in terms of writing to a ".xlsx" file, but any ideas about determining the correct file type?

I could just have the user choose when naming the file, but that seems like extra work for the user and I'm assuming that there are users who don't know what file type they'd want.

Any ideas?

Also, I'm assuming the OS is windows and the user has some version of excel, in other cases I'll just choose a default file type.

解决方案

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

You may want to add more error checking and the parsing to extract the Office version from the returned path is left as an exercise ;-)

这篇关于用java确定微软Office的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆