用java确定微软Office的版本 [英] determine version of microsoft office with java
问题描述
我可以让用户在命名文件时进行选择,但这对用户来说似乎是额外的工作,我假设有不知道的用户他们想要什么文件类型。
任何想法?
另外,我假设操作系统是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屋!