how to add Labels to an excel sheet using jexcel

how to add Labels to an excel sheet using jexcel

Previous Home Next

 

In the following example, we have shown how can you add various Labels in your worksheet using the JExcel Api. 

For creating a Label inside a workhseet of an workbook we have import the jxl.write.Label class. 
 After importing the Label class we have created an object of the Label class, and then  add the Label to the sheet by 
using the addCell(WritableCell wc) method contained in the 
jxl.write.WritableSheet class.

The following code shows how to add a Label to Excel Worksheet you have created by using the JExcel Api. In this program we have created a class named AddLabelToSheet, in which we have created a workbook to a certain location named firstsheet.xls. And in that workbook we have created a sheet named mysheet, then we have created three Labels namely lblEmpName, lblEmpID and lblSex, The constructor of the Label  consists of the three parameters ,
Label(int arg1, int arg2, String str)
where , int arg1-> is the column of the sheet
            int arg2-> is the row of the sheet
            String str -> is the name of the Label created.

Finally, we have add these Labels to the sheet by calling the addCell() method on the sheet.

 

package r4r.co.in;

import java.io.IOException;
import java.io.File;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.Label;        // importing the Label class defined in jxl.write package
public class AddLabelToSheet {

	/**
	 * @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);
	    Label lblEmpName=new Label(1,0,"EmpName");
	    Label lblEmpID=new Label(0,0,"EmpID");
	    Label lblSex=new Label(2,0,"Sex");
	    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