Example of Excel Label Format with jxl.jar

Q

Where can I find an example Java code that uses jxl.jar to add label cell format to a new Excel file?

✍: FYIcenter.com

A

You can follow these suggestions and example to add label cell format to a new Excel file with jxl.jar:

The previous tutorial 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.

// Create a cell format for Arial 10 point font 
WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10); 
WritableCellFormat arial10format = new WritableCellFormat (arial10font); 

// Create the label, specifying content and format 
Label label2 = new Label(1,0, "Arial 10 point label", arial10format); 
sheet.addCell(label2);

Cell formats objects are shared, so many cells may use the same format object, eg.

Label label3 = new Label(2, 0, "Another Arial 10 point label", arial10format); 
sheet.addCell(label3);

This creates another label, with the same format, in cell C1.

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.

// Create a cell format for Times 16, bold and italic 
WritableFont times16font = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, true); 
WritableCellFormat times16format = new WritableCellFormat (times16font); 

// Create the label, specifying content and format 
Label label4 = new Label(3,0, "Times 16 bold italic label", times16format); 
sheet.addCell(label4);

Here is the complete source code of example program JxlAddLabelFormat.java:

// Copyright (c) FYIcenter.com
import jxl.write.*;

// Example of adding formatting properties for labels in a new Excel file
public class JxlAddLabelFormat {
   public static void main(String [] args) throws Exception {
    jxl.write.WritableWorkbook workbook 
       = jxl.Workbook.createWorkbook(new java.io.File("output.xls"));
      jxl.write.WritableSheet sheet = workbook.createSheet("First Sheet", 0);

      // Create a cell format for Arial 10 point font 
      WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10); 
      WritableCellFormat arial10format = new WritableCellFormat (arial10font); 
      
      // Create the label, specifying content and format 
      Label label2 = new Label(1,0, "Arial 10 point label", arial10format); 
      sheet.addCell(label2);
      Label label3 = new Label(2, 0, "Another Arial 10 point label", arial10format); 
      sheet.addCell(label3);
      
    // Create a cell format for Times 16, bold and italic 
      WritableFont times16font = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, true); 
      WritableCellFormat times16format = new WritableCellFormat (times16font); 
      
      // Create the label, specifying content and format 
      Label label4 = new Label(3,0, "Times 16 bold italic label", times16format); 
      sheet.addCell(label4);

      workbook.write(); 
      workbook.close();
   }
}

You can compile and run the above example in a command window as shown below:

C:\fyicenter>c:\local\jdk-1.8.0\bin\javac 
   -cp .;\fyicenter\jexcelapi\jxl.jar JxlAddLabelFormat.java

C:\fyicenter>c:\local\jdk-1.8.0\bin\java 
   -cp .;\fyicenter\jexcelapi\jxl.jar JxlAddLabelFormat

The output file, output.xls, is ready for Microsoft Excel to open.

 

Example of Excel Number Format with jxl.jar

Example of Creating Excel File with jxl.jar

Java Source Code Example for jxl.jar

⇑⇑ FAQ for Java Excel API jxl.jar

2018-02-21, 2118🔥, 0💬