DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Integrate OpenOffice with Java without Installing OpenOffice

Integrate OpenOffice with Java without Installing OpenOffice

Geertjan Wielenga user avatar by
Geertjan Wielenga
·
Feb. 07, 09 · Java Zone · News
Like (0)
Save
Tweet
48.15K Views

Join the DZone community and get the full member experience.

Join For Free

Until a few days ago, I've always needed to work with the rather cumbersome Office Bean and UNO Runtime when integrating OpenOffice into a Java application. I also had to configure a whole bunch of things to force OpenOffice to play nicely with the Java integration. Two days ago, however, I found out about ODF Toolkit. It seems to be a relatively new project, independent since last year some time, though I could be wrong.

What's especially interesting is the ODFDOM: ''ODFDOM is an OpenDocument (ODF) framework. It's purpose is to provide an easy common way to create, access and manipulate ODF files, without requiring detailed knowledge of the ODF specification. It is designed to provide the ODF developer community an easy lightwork programming API, portable to any object-oriented language.''

Here's a snippet of it in action:

public static void main(String[] args) {
try {
OdfDocument odfDoc = OdfDocument.loadDocument(new File("/home/geertjan/test.ods"));
OdfFileDom odfContent = odfDoc.getContentDom();
XPath xpath = odfDoc.getXPath();
DTMNodeList nodeList = (DTMNodeList) xpath.evaluate("//table:table-row/table:table-cell[1]", odfContent, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Node cell = nodeList.item(i);
if (!cell.getTextContent().isEmpty()) {
System.out.println(cell.getTextContent());
}
}
} catch (Exception ex) {
//Handle...
}
}

Let's assume that the 'test.ods' file above has this content:

From the above, the code listing would print the following:

Cuthbert
Algernon
Wilbert

And, as a second example, here's me reading the first paragraph of an OpenOffice Text document:

public static void main(String[] args) {
try {
OdfDocument odfDoc = OdfDocument.loadDocument(new File("/home/geertjan/chapter2.odt"));
OdfFileDom odfContent = odfDoc.getContentDom();
XPath xpath = odfDoc.getXPath();
OdfParagraphElement para = (OdfParagraphElement) xpath.evaluate("//text:p[1]", odfContent, XPathConstants.NODE);
System.out.println(para.getFirstChild().getNodeValue());
} catch (Exception ex) {
//Handle...
}
}

On my classpath I have "odfdom.jar" and "xerces-2.8.0.jar". I don't necessarily have OpenOffice installed, which means I can very easily process a whole bunch of spreadsheets (or other OpenOffice output) without (a) installing OpenOffice and (b) faster than I would otherwise do, since OpenOffice doesn't need to be started up, via the Office Bean or otherwise. In fact, Aljoscha Rittner from Sepix, who told me about this project and who is using it in his commercial applications, reports that his processing has sped up to a fraction of the original, also because he doesn't need to handle the situation where OpenOffice would crash randomly in the middle of long running processes, such as during the night when there's no human interaction for restarting it.

 

Java (programming language) Integration

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Make Git Forget a Tracked File Now in .gitignore
  • C++ Creator Bjarne Stroustrup Interview
  • Container Orchestration Tools Comparison
  • Blocking Ads on Your Network Using Raspberry Pi 3 + Fedora + Pi-hole

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo