This section describes how to write out simple spreadsheet data without any formatting information via open source library JExcelApi, such as fonts or decimal places. Similarly to reading a spreadsheet, the first step is to create a writable workbook using the factory method on the Workbook class.
The next stage is to create sheets for the workbook. Again, this is a factory method, which takes the name of the sheet and the position it will occupy in the workbook. The code fragment below creates a sheet called "First Sheet" at the first position:
Once you have finished adding sheets and cells to the workbook, you call write() on the workbook, and then close the file. This final step generates the output file (output.xls in this case) which may be read by Excel. If you call close() without calling write() first, a completely empty file will be generated.
Adding Format Information:
The previous section illustrates the fundamentals of generating an Excel compatible spreadsheet using the JExcelApi. However, as it stands Excel will render the data in the default font, and will display the numbers to 3 decimal places. In order to supply formatting information to Excel, we must make use of the overloaded constructor, which takes an additional object containing the cell's formatting information (both the font and the style).
The code fragment below illustrates creating a label cell for an arial 10 point font.
Because cell formats are shared, it is not possible to change the contents of a cell format object. (If this were permitted, then changing the contents of the object could have unforeseen repurcussions on the look of the rest of the workbook). In order to change the way a particular cell is displayed, the API does allow you to assign a new format to an individual cell.
The constructors for the WritableFont object have many overloads. By way of example, the code fragment below creates a label in 16 point Times, bold italic and assigns it to position D1.
Number formatting information may be passed to the cell format object by a similar mechanism to that described for fonts. A variety of predefined number formats are defined statically. These may be used to format numerical values as follows:
It's possible for a user to define their own number formats, by passing in a number format string. The string passed in should be in the same format as that used by thejava.text.DecimalFormat class. To format a number to display up to five decimal places in cell C5, the following code fragment may be used:
Dates are handled similarly to numbers, taking in a format compatible with that used by the java.text.SimpleDateFormat class. In addition, several predefined date formats are specified in the jxl.write.DateFormat class.
As a brief example, the below code fragment illustrates placing the current date and time in cell A7 using a custom format:
As with numbers, font information may be used to display the date text by using the overloaded constructors on WritableCellFormat.
For a more extensive example of writing spreadsheets, the demonstration program Write.java should be studied. In addition to the functionality described above, this program tests out a variety of cell, formatting and font options, as well as displaying cells with different background and foreground colours, shading and boundaries.