Getting Started with OpenXLS
This section is intended to get you started quickly programming with OpenXLS.
OpenXLS is designed for use by programmers familiar with the Java syntax, the concepts of file templates, and spreadsheets.
Basic use of the API requires the following steps:
-
Copy OpenXLS.jar and openxls.lic to a project directory. Make sure that the jar is in the classpath of your project. The OpenXLS.jar file needs to be on your classpath in order for the JVM to find the OpenXLS class files.
-
In your Java code, import the
com.valkyrlabs.OpenXLS.*
package. -
Create a new
WorkBookHandle
. This can either be a new, empty WorkBook with three sheets, or you can parse an existing XLS file in the form of a byte array from a file or other data source. Review theWorkBookHandle
constructors in the API documentation to see whichWorkBookHandle
is appropriate for your application. -
Work with a
WorkSheet
by using theWorkBookHandle.getWorkSheet(String sheetname)
method. You must catch theWorkSheetNotFoundException
in case the expected sheet does not exist already in the file. -
Access Cell values using the
WorkSheetHandle.getCell(Sheetname:CellAddress)
method. You must catch theCellNotFoundException
in case the expected Cell does not exist already in the file. You can add a new Cell to the sheet and get aCellHandle
with one line of code by using theCellHandle WorkSheetHandle.add(Object ob, String address)
method. -
Set and get the value of Cells using
CellHandle.setCellVal(Object ob)
andObject CellHandle.getCellVal()
methods. -
Stream the WorkBook bytes to an output file using the
WorkBookHandle.getBytes()
method. If you are writing a web application, you can send the bytes over aServletResponse
to a web browser or you can write them out to a file or to any byte array consumer.