|
Due to the need of the project, we need to read the document in OpenOffice Java programs, and data processing. This article describes how to use the ODF Toolkit to read the contents of the OpenOffice SpreadSheet.
1. OpenOffice Spreedsheet document
The above documents are also included in the source code for this article.
OpenOffice document is actually stored in XML format that contains the document content and format control.
If you are using Unzip unzip tool, you will find that after decompression, a OpenOffice document actually contains the following elements
2. Download ODF4j
Odf4j is used to read OpenOffice documents (ODF) pure Java toolkit. At present, although not yet officially released, but already it has a deal with the basic functions of OpenOffice document.
Use Odf4j, Java programmers can easily create, modify OpenOffice documents.
3. Use ODF4j read OpenOffice document
Use ODF4j read OpenOffice document consists of two levels: Package Layer and Document Layer.
Package Layer
In the Package Layer, OpenOffice documents in a variety of resources as a named resource to deal with. Usually at this level for operating binary files, such as pictures.
Document Layer
In the Document Layer, is on the main content of the document operation. At this level, the content of the document is to be operated as a hierarchy, because the content of the document is saved in an XML file mode, it can be very easy to DOM approach to the operation. The following example will read a Document Layer OpenOffice Spreadsheet document.
4. Read the document OpenOffice Spreadsheet
import org.openoffice.odf.OdfPackage;
import org.openoffice.odf.OpenDocumentFactory;
import org.openoffice.odf.spreadsheet.SpreadsheetDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
......
OdfPackage odfPackage;
odfPackage = (SpreadsheetDocument) OpenDocumentFactory.load (path);
Document doc = odfPackage.getDocument (OdfPackage.STREAMNAME_CONTENT);
Element root = doc.getDocumentElement ();
......
Source code
After obtaining root Element, we can read the image reading xml files as OpenOffice document content.
5. The result of the program
Download the article source into the root directory NetBean Project, and then run: ant run. Operating results
References: Element
1. ODF4j: http://wiki.services.openoffice.org/wiki/Odf4j
2. Source code for this article. |
|
|
|