我需要将OpenDocument电子表格(.ods
)转换为Excel兼容文件(.xls
或.xlsx
)
我知道可以使用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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句