how to use a Number, NumberFormat, NumberFormats classes in jexcel
how to use a Number, NumberFormat, NumberFormats classes in jexcel
In this example we will see how can we add numbers and format the number styles on the excel sheet using the JExcel Api.
To add the Number cells in your worksheet of a ceratin workbook firsty you will need to import the jxl,.write.Number class and for providing the different types of formatting you also have to import the jxl.write.NumberFormats class.
In this example we have created a Number fields inside a sheet. We have called the constructor of the Number class in which we passed the various parameters which are required for creating a Number field. We have also used the NumberFormat class to format the numbers.
In this code you will see how you can add a number field in your worksheet using the jexcel api.
package r4r.co.in;
import java.io.IOException;
import java.io.File;
import jxl.Workbook;
import jxl.write.NumberFormats;
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.*;
import jxl.write.Number;
import jxl.write.NumberFormat;
public class UseNumberClassInWorkbook {
/**
* @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.setBackground(Colour.PINK);
cfobj.setWrap(true);
Label lblFloatNum1=new Label(0,0,"FloatNUM1",cfobj); // creating a label named FloatNUM1
Label lblFloatNum2=new Label(1,0,"FloatNUM2",cfobj); // creating a label named FloatNUM2
Label lblFloatNum3=new Label(2,0,"Float3dps", cfobj); // creating a label named Float3dps
sheet.addCell(lblFloatNum1); // adding labels to sheet
sheet.addCell(lblFloatNum2);
sheet.addCell(lblFloatNum3);
WritableCellFormat cf1obj=new WritableCellFormat(NumberFormats.FLOAT); // providing a format to cell data which is of type float
Number numobj=new Number(0,1,3.5556434356, cf1obj); // created an object of Number class
sheet.addCell(numobj);
numobj=new Number(0,2,4.56453453,cf1obj);
sheet.addCell(numobj);
numobj=new Number(1,1,2.4564465, cf1obj);
sheet.addCell(numobj);
numobj=new Number(1,2,7.3453455, cf1obj);
sheet.addCell(numobj);
NumberFormat nf=new NumberFormat("#.###"); // this format constraints the number upto 3 decimal points
WritableCellFormat cf2obj=new WritableCellFormat(nf);
numobj=new Number(2,1,6.353454,cf2obj);
sheet.addCell(numobj);
numobj=new Number(2,2,1.454567677, cf2obj);
sheet.addCell(numobj);
workbook.write();
workbook.close();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(WriteException e)
{
e.printStackTrace();
}
}
}