【问题标题】:How can I convert a .ods file to an .xls file如何将 .ods 文件转换为 .xls 文件
【发布时间】:2016-05-18 15:25:20
【问题描述】:

我需要将 OpenDocument 电子表格 (.ods) 转换为 Excel 兼容文件(.xls.xlsx

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

【问题讨论】:

  • 你的问题更适合softwarerecs.stackexchange.com这样的地方
  • xlsx 就像 ods 一种 zip 格式,内部包含 XML、图像文件等。所以转换不是太难。 MS Word 也可以处理 .ods,但可能不是开箱即用的。
  • @JoopEggen 是的,没错,所以我猜有人,在某个地方已经建立了一个图书馆什么的,我不可能是唯一的!
  • 转换中需要多少保真度?只是价值观和公式?还是所有带有图像、格式、图表等的东西?
  • 我看到了您的apache-poi 标签,但 POI 不读取或写入 .ods 文件。

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


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 2014-03-13
    • 1970-01-01
    • 2010-12-06
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多