X

Geertjan's Blog

  • June 20, 2006

OpenOffice.org API (Part 1)

Geertjan Wielenga
Product Manager
Again with the help of Wouter (see yesterday's blog entry for details), I'm working on some more early experiments with the OpenOffice.org API. Here's how I've extended an application built atop the NetBeans Platform—data is retrieved from a Java DB database at start up and then, when the "Report" button is clicked, the first column in the application's table is written to a table in an Open Office Writer document:

For a small taste of what's going on behind the scenes (because, once I've stopped experimenting, a tutorial or two should come out of this), here's all the code (with comments), from the first line to the last, found under the "Report" button:

//Here we set an Open Office helper class, containing some useful methods:
OOoHelper oooHelper = new OOoHelper();
//Here we get an executable from the Options window:
settings = OfficeDocSettings.getSettings();
//Here we set the (currently hardcoded) arguments that will start Open Office in "listen mode":
String arguments = "-accept=\\"socket,host=\\"localhost\\",port=9000;urp;StarOffice.ServiceManager\\"";
try {
//Here the NetBeans process is defined for starting up Open Office:
NbProcessDescriptor process = new NbProcessDescriptor(settings.getValue(settings.KEY_EXECUTABLE),arguments);
//Here the process is executed:
process.exec();
//Here a call is made to a method that will connect the application to Open Office:
Object desktop = oooHelper.remoteConnect("localhost","9000");
//Here various Open Office objects are created:
XComponent writerComponent = oooHelper.getWriterComponent(desktop);
XTextDocument xTextDocument = oooHelper.getDocument(writerComponent);
XText xText = oooHelper.getXText(xTextDocument);
//Here we call the method that creates the table:
XTextTable xTextTable = createExampleTable(xTextDocument);
} catch (Exception ex) {
ex.printStackTrace();
}

This is all pretty cool stuff and, as Wouter wrote to me this morning: The sky is the limit.

Join the discussion

Comments ( 9 )
  • Chuck Davis Tuesday, June 20, 2006
    Geertjan:
    How many jar files did you have to add to your classpath? Last time I looked java use of OOo was totally undeployable due to the number of jars everything was scattered throughout -- there were something like 20 jars that had to be added to he classpath. If your buddy at OOo wants to do something good for java/OOo tell him to get everything we need into one jar we can add to the classpath of our workstations!
  • Wouter van Reeven Wednesday, June 21, 2006
    Hi Chuck,
    At the moment only a few jars and some classes are needed to be able to connect to OOo. First of all, you need to include the directory from the OOo SDK containing the com.sun.star.lib.loader package. On Windows this package resides in the classes directory contained in the OOo SDK. On Debian Linux, this package can be found in /usr/share/java/openoffice
    Besides that, you need to make sure these jars are on your classpath
    juh.jar
    jurt.jar
    ridl.jar
    unoil.jar
    and maybe also
    sandbox.jar
    All these jars can be found in the program/classes directory in the directory in which OOo is installed. On Windows the default location is C:\\Program Files\\OpenOffice.org 2.0 and on Debian it is /usr/lib/openoffice
    HTH, Wouter van Reeven
  • Geertjan Wednesday, June 21, 2006
    Plus, if you use the NetBeans Execution API (i.e., the NbProcessDescriptor bit in the snippet above), you don't even need the com.sun.star.lib.loader package. So, in answer to Chuk's question, I only need to use juh.jar, jurt.jar, ridl.jar, unoil.jar, and sandbox.jar. That's only 5. Not bad at all.
  • Geertjan Wednesday, June 21, 2006
    Sorry, Chuck, I didn't mean to call you "Chuk". That's someone else. :-) His name is Chuk.
  • Chuck Davis Wednesday, June 21, 2006
    Hi Guys:
    I still maintain that everything we need to use OOo with java needs to be put in one jar file. At least it's going to have to be that simple if I'm going to use it. I looked at using OOo for reporting but deployment was just too much so I learned the java printing api instead. Please, Wouter, do what you can to get the OOo developers to give us everything in one jar file!
    Thanks for your efforts. I think this is something that really, really needs to be done!
    Chuck
    P.S. Geertjan, you can call me anything. Just don't call me late for dinner!
  • Geertjan Wednesday, June 21, 2006
    Hi Chuck, I agree that what you propose would be wonderful. The world of One-JAR-APIs is one that I would welcome too. I'll do what I can, with all the power that I have, to move the pieces on the board to make this happen for the OOo API. Then again, with all the power that I have the only pieces on the board that you'll ever see me moving come from a game you might have heard of... chess.
  • Wouter van Reeven Thursday, June 22, 2006
    Chuck,
    I doubt if it ever will be possible to put everything in one jar file. There simply is too much in OOo that is necessary for calling it from Java.
    However, it IS possible to create one jar file and connect to a running OOo instance via a network connection. Would that be sufficient for you?
    Yesterday I created a very simple jar that contained my main code. With a batch file calling java with the appropriate command line switches I was able to start up OOo and connect to it. Is that what you are after?
    Greets, Wouter
  • Edson Richter Sunday, June 25, 2006
    I don't think it's necessary to put everything in one Jar. Just check JasperReports - there is dozens of jars necessary to generate reports... 5 is just so tiny, it's almost perfect (well, perfect will be when it came in one Jar with NetBeans ;-) )
  • Manoj Tuesday, June 14, 2016

    Hi,

    I want to export directory Path Structure in OpenDocument spreadsheet (openOffice.org Calc). Want to Know how to create SpreadSheet in OpenOffice.org and how to export directory structure Using JAVA.

    Thanks

    Manoj


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.