如何使用OpenOffice SDK在Java中生成XLS、DOC、PPT及ODS文件?
用OpenOffice SDK在Java中生成ODS/ODT/ODP文档(无头模式,适配AIX+WebSphere)
我完全懂你的痛点——服务器端生成文档绝对不能拉起桌面应用,而且要在AIX的WebSphere环境下稳定运行,还要支持表格、文字、演示文档三种格式的生成。之前用BootstrapSocketConnector会启动OpenOffice应用,这在服务器端完全不可行,咱们得换成连接已运行的无头OpenOffice服务的方式来实现。
前置准备:在AIX上启动无头OpenOffice服务
首先要在AIX服务器上提前启动OpenOffice的后台服务,用无头模式,这样Java代码只需要连接这个服务就行,不会启动新应用。执行以下命令:
soffice --headless --accept="socket,host=localhost,port=8100;urp;" --nofirststartwizard &
可以把这个命令做成开机自启的脚本,确保服务一直运行。
Java示例代码
1. 通用连接工具类(核心:连接无头服务,避免启动应用)
这个类负责连接已运行的OpenOffice服务,替代之前的Bootstrap方式:
import com.sun.star.comp.helper.Bootstrap; import com.sun.star.connection.XConnection; import com.sun.star.connection.XConnector; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XDesktop; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; public class OOConnectionUtil { private static final String OO_SERVICE_URL = "uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext"; public static XComponentContext getComponentContext() throws Exception { // 获取本地连接器 XConnector connector = Bootstrap.createConnector(); // 连接到已启动的OpenOffice服务 XConnection connection = connector.connect(OO_SERVICE_URL); // 获取组件上下文 XComponentContext context = UnoRuntime.queryInterface(XComponentContext.class, Bootstrap.createInitialComponentContext(connection)); return context; } public static XComponentLoader getComponentLoader() throws Exception { XComponentContext context = getComponentContext(); XMultiComponentFactory factory = context.getServiceManager(); XDesktop desktop = UnoRuntime.queryInterface(XDesktop.class, factory.createInstanceWithContext("com.sun.star.frame.Desktop", context)); return UnoRuntime.queryInterface(XComponentLoader.class, desktop); } }
2. 生成ODS(电子表格)示例
这个示例会创建一个带数据的ODS表格,保存到指定路径,之后可以返回给前端下载:
import com.sun.star.beans.PropertyValue; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XStorable; import com.sun.star.lang.XComponent; import com.sun.star.sheet.XSpreadsheet; import com.sun.star.sheet.XSpreadsheetDocument; import com.sun.star.sheet.XSpreadsheets; import com.sun.star.table.XCell; import com.sun.star.uno.UnoRuntime; import java.io.File; public class ODSGenerator { public static void generateODS(String outputPath) throws Exception { // 获取组件加载器 XComponentLoader loader = OOConnectionUtil.getComponentLoader(); // 创建空白电子表格文档 PropertyValue[] loadProps = new PropertyValue[0]; XComponent doc = loader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps); // 获取表格对象 XSpreadsheetDocument spreadsheetDoc = UnoRuntime.queryInterface(XSpreadsheetDocument.class, doc); XSpreadsheets sheets = spreadsheetDoc.getSheets(); XSpreadsheet sheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheets.getByName("Sheet1")); // 填充单元格数据 XCell cellA1 = sheet.getCellByPosition(0, 0); cellA1.setValue(100); XCell cellA2 = sheet.getCellByPosition(0, 1); cellA2.setFormula("=A1*2"); XCell cellB1 = sheet.getCellByPosition(1, 0); cellB1.setString("报表标题"); // 保存文档到指定路径 XStorable storable = UnoRuntime.queryInterface(XStorable.class, doc); String url = "file:///" + new File(outputPath).getAbsolutePath().replace("\\", "/"); PropertyValue[] saveProps = new PropertyValue[1]; saveProps[0] = new PropertyValue(); saveProps[0].Name = "Overwrite"; saveProps[0].Value = Boolean.TRUE; storable.storeAsURL(url, saveProps); // 关闭文档 doc.dispose(); } // 测试调用 public static void main(String[] args) { try { generateODS("/tmp/report.ods"); System.out.println("ODS生成成功!"); } catch (Exception e) { e.printStackTrace(); } } }
3. 生成ODT(Word文档)示例
只需要修改文档工厂标识和内容操作部分:
import com.sun.star.beans.PropertyValue; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XStorable; import com.sun.star.lang.XComponent; import com.sun.star.text.XText; import com.sun.star.text.XTextDocument; import com.sun.star.uno.UnoRuntime; import java.io.File; public class ODTGenerator { public static void generateODT(String outputPath) throws Exception { XComponentLoader loader = OOConnectionUtil.getComponentLoader(); // 创建空白文字文档 XComponent doc = loader.loadComponentFromURL("private:factory/swriter", "_blank", 0, new PropertyValue[0]); // 获取文本对象并写入内容 XTextDocument textDoc = UnoRuntime.queryInterface(XTextDocument.class, doc); XText text = textDoc.getText(); text.setString("这是生成的Word文档内容\n第二行内容\n"); // 保存文档 XStorable storable = UnoRuntime.queryInterface(XStorable.class, doc); String url = "file:///" + new File(outputPath).getAbsolutePath().replace("\\", "/"); PropertyValue[] saveProps = new PropertyValue[1]; saveProps[0] = new PropertyValue(); saveProps[0].Name = "Overwrite"; saveProps[0].Value = Boolean.TRUE; storable.storeAsURL(url, saveProps); doc.dispose(); } }
4. 生成ODP(演示文档)示例
同理,更换工厂标识为private:factory/simpress:
import com.sun.star.beans.PropertyValue; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XStorable; import com.sun.star.lang.XComponent; import com.sun.star.presentation.XSlide; import com.sun.star.presentation.XSlideDocument; import com.sun.star.presentation.XSlides; import com.sun.star.uno.UnoRuntime; import java.io.File; public class ODPGenerator { public static void generateODP(String outputPath) throws Exception { XComponentLoader loader = OOConnectionUtil.getComponentLoader(); // 创建空白演示文档 XComponent doc = loader.loadComponentFromURL("private:factory/simpress", "_blank", 0, new PropertyValue[0]); // 获取第一张幻灯片并操作(这里简单设置标题) XSlideDocument slideDoc = UnoRuntime.queryInterface(XSlideDocument.class, doc); XSlides slides = slideDoc.getSlides(); XSlide firstSlide = UnoRuntime.queryInterface(XSlide.class, slides.getByIndex(0)); // 注:若要修改幻灯片内容,需要获取形状对象,这里简化示例 firstSlide.setName("首页幻灯片"); // 保存文档 XStorable storable = UnoRuntime.queryInterface(XStorable.class, doc); String url = "file:///" + new File(outputPath).getAbsolutePath().replace("\\", "/"); PropertyValue[] saveProps = new PropertyValue[1]; saveProps[0] = new PropertyValue(); saveProps[0].Name = "Overwrite"; saveProps[0].Value = Boolean.TRUE; storable.storeAsURL(url, saveProps); doc.dispose(); } }
WebSphere适配注意事项
- 类路径配置:把OpenOffice SDK的
ridl.jar、juh.jar、jurt.jar、unoil.jar这几个核心jar包放到WebSphere的应用库中,或者打包到你的WAR/EAR包中。 - 文件权限:确保WebSphere的运行用户对生成文档的目录有读写权限,AIX上注意文件系统的权限设置。
- 服务稳定性:可以用
ps命令监控OpenOffice服务进程,确保它一直运行;如果进程意外退出,可以写个监控脚本自动重启。
内容的提问来源于stack exchange,提问作者user3544081
相关产品推荐
相关产品推荐

