formatting the font of a Label in jexcel sheet

formatting the font of a Label in jexcel sheet

Previous Home Next

 

In this example we will show you how you can format the font and text of the labels and cells of an excel worksheet created by using JExcel Api.

For formatting the font we have to import the jx,.write.WritableFont class. And in order to format a cell the sheet you have to import the jxl.write.WritableCellFormat Class. These both classes contains various methods which are useful in formatting the cells of the created sheet.
 In this example we are formatting the fonts and cells of an excel sheet. Firstly we have called  a constructor of the WritableFont class. The constructor has different parameters passed in it. eg.

WritableFont(Font f)
It is the copy constructor which is available publicly.

WritableFont(WritableFont.FontName fnt)
This constructor creates a default font of the specified face and with default point size.

WritableFont(WritableFont.FontName fnt, int pointsize)
It constructs of font of the desired face and the size of the point specified by point size value.

WritableFont(WritableFont.FontName fnt, int pointsize, WritableFont.BoldStyle bldstyl)
It reates a font of the specified face, point size and with bold style.

WritableFont(WritableFont.FontName fnt, int pointsize, WritableFont.BoldStyle bldstyl, boolean italicisation)
This constructor creates a font of the specified face, point size, bold style and italicised option which is boolean ie true or false.

WritableFont(WritableFont.FontName fnt, int pointsize, WritableFont.BoldStyle bldstyl, boolean italicisation, UnderlineStyle undrlnstyl)
This constructor creates a font of the specified face, point size, bold style, italicisation and underline style for underlining the text.

WritableFont(WritableFont.FontName fnt, int pointsize, WritableFont.BoldStyle bldstyl, boolean italicisation, UnderlineStyle undrlnstyl, Colour c)
It creates a font of the specified face, point size, bold style, italicisation, underline style and colour.

WritableFont(WritableFont.FontName fnt, int pointsize, WritableFont.BoldStyle bldstyl, boolean italicisation, UnderlineStyle undrlnstyls, Colour c, ScriptStyle scptstyl)
This constructorc reates a font of the specified face, point size, bold style, italicisation, underline style, colour, and script style.

Then we passed the object of the WritableFont to the constructor of the WritableCellFormat class, and then we called the method setWrap(Boolean b) on the object of WritableCellFormat to enable cell formatting.

In the following we have created a class named FormatFontInWorkbook in which we added some labels. Then we formatted the fonts of those labels and then, we enable the cell formatting by using the setWrap(Boolean b) method on the WritableCellFormat object.

 

package r4r.co.in;

import java.io.IOException;
import java.io.File;
import jxl.Workbook;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.Label;
import jxl.format.*; 
public class FormatFontInWorkbook {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException, WriteException{
		// TODO Auto-generated method stub

	try
	{
		WritableWorkbook workbook=Workbook.createWorkbook(new File("F:/JAVA PROJECTS/Jexcel/src/r4r/co/in/firstsheet.xls"));
	    WritableSheet sheet=workbook.createSheet("mysheet", 0);
	    WritableFont wfobj=new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD);
	    WritableCellFormat cfobj=new WritableCellFormat(wfobj);
	    cfobj.setWrap(true);
	    
	    Label lblEmpName=new Label(1,0,"EmpName", cfobj);
	    Label lblEmpID=new Label(0,0,"EmpID", cfobj);
	    Label lblSex=new Label(2,0,"Sex", cfobj);
	    sheet.addCell(lblEmpName);
	    sheet.addCell(lblEmpID);
	    sheet.addCell(lblSex);
	    workbook.write();
	    workbook.close();
	}
	catch(IOException e)
	{
		e.printStackTrace();
	}
	catch(WriteException e)
	{
		e.printStackTrace();
	}
	}

}


Previous Home Next