如何将.ods文件转换为.xls文件

时间:2016-05-18 15:25:20

标签: java excel apache-poi libreoffice libreoffice-calc

我需要将OpenDocument电子表格("~/orders/orderAction.aspx?orderID={0}" )转换为Excel兼容文件(.ods.xls

我知道它可能using libreoffice cli。我想用Java做这件事。我知道Java可以运行命令行进程,但我更喜欢仅JVM解决方案(不需要环境上的libreoffice)。有没有可以提供帮助的开源库?

1 个答案:

答案 0 :(得分:1)

我最后打电话给libreoffice。

File outputDirectory = source.getParentFile();
String[] command = {
        "/usr/bin/libreoffice",
        "--headless",
        "--invisible",
        "-env:UserInstallation=file://" + SystemUtils.getJavaIoTmpDir().getAbsolutePath(), // prevent conversion to fail or just do nothing if libreoffice is already running
        "--convert-to",
        "ods",
        "--outdir",
        outputDirectory,
        source.getAbsolutePath()
};
try {
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.directory(outputDirectory);
    Process process = processBuilder.start();
    int returnValue = process.waitFor();
    if (returnValue == 0) {
        log.debug("process ended successfully");
    } else {
        log.error("process ended with error code " + returnValue);
    }
} catch (IOException e) {
    throw new UncheckedIOException("cannot convert " + source, e);
}