Write out simple spreadsheet data with formatting information, such as fonts or decimal places.

Write out simple spreadsheet data with formatting information, such as fonts or decimal places.

Previous Home Next

 

Write out simple spreadsheet data with formatting information, such as fonts or decimal places.

The various classes which we have discussed earlier have been used to create the following spreadsheet.
 The following code snippet will show you how to create a spread sheet having the formatting information,

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);

In above code we have created a workbook and then created a sheet named mysheet, and after that we have shown how to include the formatting  information in your excel worksheet.


 

Previous Home Next