|
Examples - HelloExcel
A classic start for your exploration of the possibilities of xelem.
HelloExcel.java wil produce a file with the name
HelloExcel.xls and guess what's in the first cell of the
first worksheet? Hello Excel!
HelloExcel class- and sourcefile can be found in the directory examples ,
provided with the xelem-distribution.
See: download.
import java.util.Collection; import java.util.Iterator; import nl.fountain.xelem.XSerializer; import nl.fountain.xelem.XelemException; import nl.fountain.xelem.excel.Workbook; import nl.fountain.xelem.excel.ss.XLWorkbook; public class HelloExcel { public static void main(String[] args) throws XelemException { Workbook wb = new XLWorkbook("HelloExcel"); wb.addSheet().addCell("Hello Excel!"); new XSerializer().serialize(wb); Collection warnings = wb.getWarnings(); System.out.println("Created '" + wb.getFileName() + "' with " + warnings.size() + (warnings.size() == 1 ? " warning." : " warnings.")); for (Iterator iter = warnings.iterator(); iter.hasNext();) { System.out.println(iter.next()); } } } The workbook created by HelloExcel.java can be seen here. And this is the same file seen in xml-view. Creation of the workbookThe creation of the workbook HelloExcel.xls took place in just three lines of code:Workbook wb = new XLWorkbook("HelloExcel"); wb.addSheet().addCell("Hello Excel!"); new XSerializer().serialize(wb);In the first line a new XLWorkbook was constructed with the name HelloExcel. The add-methods will return an instance of the thing that was added and this ensures compact coding. But, after all, this is a matter of taste. In the second line a Worksheet was added to the Workbook which got the default name 'Sheet1'. The CellPointer of a newly constructed sheet will be at row 1, column 1, and that's where the newly constructed Cell was added. Finally, the data of this cell was set to be 'Hello Excel!' The third line serializes the workbook to a file. Since we didn't set a file name on the workbook ecplicitly the file got the name of 'HelloExcel.xls'; the name of the workbook extended with '.xls'. Printing the warningsWhen serializing the workbook the XSerializer calls on the workbook's method createDocument, which can produce warnings. The rest of the code prints these warnings (if there are any). In the warnings-collection you might find things like the XFactory, yet another class involved in the creation of the document, couldn't find the configuration file. Or the definition of a used style was not found. Since -most of the time- these errors are not fatal, these things are not handled as exceptions.Wrapper: XelemExceptionThe XelemException you find in the declaration of the main-method could be thrown in line three by the XSerializer serializing the workbook. XelemException acts as a wrapper around several exceptions that might be thrown during the construction of XSerializer and the execution of it's methods.Alternative ways to serialize the workbookThe XSerializer-class was added to xelem for convenience. There are other ways to serialize a org.w3c.dom.Document. You might use the org.apache.xalan.serialize.SerializerToXML, also part of the Java Standard Edition. Here's how that might look. The variablewb again points to a XLWorkbook.
... Properties props = new Properties(); props.setProperty(OutputKeys.INDENT, "yes"); props.setProperty(OutputKeys.ENCODING, "US-ASCII"); SerializerToXML s = new SerializerToXML(); s.init(System.out, props); s.serialize(wb.createDocument()); ...This will serialize the workbook to System.out.
12-12-2004 |