如何将 .ods 文件转换为 .xls 文件
How can I convert a .ods file to an .xls file
我需要将 OpenDocument 电子表格 (.ods
) 转换为 Excel 兼容文件(.xls
或 .xlsx
)
我知道这是可能的 using libreoffice
cli。我想使用 Java 来做这件事。我知道 Java 可以 运行 命令行进程,但我更喜欢仅使用 JVM 的解决方案(不需要在环境中使用 libreoffice)。是否有任何开源库可以提供帮助?
我最后调用了 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);
}
我需要将 OpenDocument 电子表格 (.ods
) 转换为 Excel 兼容文件(.xls
或 .xlsx
)
我知道这是可能的 using libreoffice
cli。我想使用 Java 来做这件事。我知道 Java 可以 运行 命令行进程,但我更喜欢仅使用 JVM 的解决方案(不需要在环境中使用 libreoffice)。是否有任何开源库可以提供帮助?
我最后调用了 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);
}