numéro6 numéro6 - 1 year ago 220
Java Question

How can I convert a .ods file to an .xls file

I need to convert a OpenDocument spreadsheet (

) to an Excel compatible-file (

I know it's possible using
. I would like to do this exact thing using Java. I know that Java could run the command-line process but I would prefer a JVM-only solution (not requiring libreoffice on the environment). Is there any open-source library that could help?

Answer Source

I ended up by calling libreoffice.

File outputDirectory = source.getParentFile();
String[] command = {
        "-env:UserInstallation=file://" + SystemUtils.getJavaIoTmpDir().getAbsolutePath(), // prevent conversion to fail or just do nothing if libreoffice is already running
try {
    ProcessBuilder processBuilder = new ProcessBuilder(command);;
    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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download