简体   繁体   English

用 java 确定 microsoft office 的版本

[英]determine version of microsoft office with java

I wrote a program that creates a set of data that is outputted to an excel spreadsheet.我编写了一个程序来创建一组数据,这些数据输出到 excel 电子表格。 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.我最初使用 jexcel 库将数据写入文件,但我想更新程序,以便它可以检查是否应该创建“.xls”或“.xlsx”文件,然后写入适当的文件类型。 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? Apache POI 似乎是写入“.xlsx”文件的最佳选择,但是关于确定正确文件类型的任何想法?

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.另外,我假设操作系统是 windows 并且用户有一些版本的 excel,在其他情况下我将只选择默认文件类型。

One way is to call the Windows ASSOC and FTYPE commands, capture the output and parse it to determine the Office version installed.一种方法是调用 Windows ASSOC 和 FTYPE 命令,捕获 output 并对其进行解析以确定安装的 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 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;-)您可能希望添加更多错误检查,并将从返回的路径中提取 Office 版本的解析留作练习;-)

You can search in the registry for the key:您可以在注册表中搜索密钥:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App 路径

This will probably require some work, as evidenced by this question:正如这个问题所证明的那样,这可能需要一些工作:

read/write to Windows Registry using Java 使用 Java 读取/写入 Windows 注册表

Take a look at OfficeVer .看看OfficeVer

You can implement it to your script or use it for code analysis.您可以将其实现到您的脚本或将其用于代码分析。 It's cross-platform much like Java, so compiling it and implementing it directly shouldn't be a big deal.它是跨平台的,很像 Java,所以直接编译和实现应该没什么大不了的。 It works by extracting.docx and xlsx files and then reading the version, as well as reading directly from.doc and.xls files.它的工作原理是提取 .docx 和 xlsx 文件,然后读取版本,以及直接从 .doc 和 .xls 文件中读取。 OfficeVer as well has extended their support to.pdf files (current version as of time of writing is 1.03.1) OfficeVer 也已将其支持扩展到.pdf 文件(截至撰写本文时的当前版本为 1.03.1)

If you're willing to dive into the registry (eg with jregistrykey ) a translated version of this PowerShell script should do what you want.如果您愿意深入研究注册表(例如使用jregistrykey ),则此 PowerShell 脚本的翻译版本应该可以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM