OpenOffice 电子表格监听器
Posted
技术标签:
【中文标题】OpenOffice 电子表格监听器【英文标题】:OpenOffice spreadsheet listener 【发布时间】:2013-01-21 16:58:46 【问题描述】:我想使用 Java 跟踪用户对 LibreOffice / OpenOffice 电子表格的输入。目前,我从the first LibreOffice examples 开始,并想附上类似的东西
com.sun.star.chart.XChartDataChangeEventListener
(见here),也许是一个
com.sun.star.container.XContainerListener
到电子表格。
我在尝试将 XSpreadsheetDocument 转换为 XContainer 时收到 NullPointerException。 我发现 API 文档非常难以浏览,并且非常感谢您提供以下提示:
找出 XSpreadsheetDocument 的类型层次结构, 将哪个侦听器附加到哪个接口,以及 该接口的工厂链【问题讨论】:
【参考方案1】:通过猜测,我发现从private:factory/scalc
加载的XComponent
属于可以转换为接口com.sun.star.util.XModifyBroadcaster
的服务,从而可以安装com.sun.star.util.XModifyListener
。
这是一个工作示例:
import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.comp.helper.BootstrapException;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.EventObject;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XModifyBroadcaster;
public class CalcListener
public static void main( String[] args)
try
// Lots of verbose code to get a Document instance
final XComponentContext xRemoteContext = Bootstrap.bootstrap();
if( xRemoteContext == null )
System.err.println("ERROR: Could not bootstrap default Office.");
return;
final XMultiComponentFactory xRemoteServiceManager = xRemoteContext.getServiceManager();
final Object desktop = xRemoteServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop", xRemoteContext );
final XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);
final PropertyValue[] loadProps = new PropertyValue[0];
final XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps);
// Cast to interface Document, to do anything useful later
final XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSpreadsheetComponent);
// Cast to interface XModifyBroadcaster to attach listener
final XModifyBroadcaster messageHost = (XModifyBroadcaster) UnoRuntime.queryInterface(XModifyBroadcaster.class, xSpreadsheetDocument );
messageHost.addModifyListener( new XModifyListener()
public void modified(EventObject eo)
System.out.println("modified:"+eo.Source);
public void disposing(EventObject eo)
System.out.println("disposing:"+eo);
);
// Do possibly useful stuff
// ...
catch (BootstrapException | Exception ex)
System.err.println("bad things happened:"+ex);
【讨论】:
这次我不得不手动将 libjpipe.so (/usr/lib/ure/lib) 的路径添加到类路径中。 为什么 LibreOffice 不提供这么清晰的东西?做得好,谢谢。以上是关于OpenOffice 电子表格监听器的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Perl 创建或读取 OpenOffice 电子表格?