Friday 9 January 2015

jxl: Add numbers to xls sheet


You can add numbers to .xls sheet by creating Number object.

Number number = new Number(3, 4, 3.1459);
sheet.addCell(number);

public class WorkBookNotInitializedException extends RuntimeException{

    WorkBookNotInitializedException(){
        super("work book is not initialized");
    }
}

import java.io.IOException;
import jxl.*;
import jxl.write.*;
import java.io.File;

public class XLSWritableUtils {
    WritableWorkbook workbook = null;

    public XLSWritableUtils(String fileName){
        try {
            workbook = Workbook.createWorkbook(new File(fileName));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void checkWorkbookInitialized(){
        if(workbook == null)
            throw new WorkBookNotInitializedException();
    }

    public void createSheet(String sheetName, int index){
        checkWorkbookInitialized();
        workbook.createSheet(sheetName, index);
    }

    public void addString(WritableSheet sheet, int column, int row, String data){
        Label label = new Label(column, row, data);
        try{
            sheet.addCell(label);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    public void addString(String sheetName, int column, int row, String data){
        checkWorkbookInitialized();
        WritableSheet sheet = workbook.getSheet(sheetName);
        addString(sheet, column, row, data);
    }

    public void addString(int sheetIndex, int column, int row, String data){
        checkWorkbookInitialized();
        WritableSheet sheet = workbook.getSheet(sheetIndex);
        addString(sheet, column, row, data);
    }
    
    public void addNumber(WritableSheet sheet, jxl.write.Number number){
        try{
            sheet.addCell(number);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    public void addNumber(String sheetName, jxl.write.Number number){
        checkWorkbookInitialized();
        WritableSheet sheet = workbook.getSheet(sheetName);
        addNumber(sheet, number);
    }

    public void addNumber(int sheetIndex, jxl.write.Number number){
        checkWorkbookInitialized();
        WritableSheet sheet = workbook.getSheet(sheetIndex);
        addNumber(sheet, number);
    }

    public void close(){
        checkWorkbookInitialized();
        try{
           workbook.write();
           workbook.close();
        }
        catch(Exception e){
            
        } 
    }
}


import jxl.write.Number;

public class TestUtils {
    public static void main(String[] args) {
       String fileName = "data.xls";
       XLSWritableUtils utils = new XLSWritableUtils(fileName);
       utils.createSheet("constants", 0);

       Number number = new Number(1, 0, 3.1459);
       utils.addString("constants", 0, 0, "PI");
       utils.addNumber("constants", number);
     
       utils.close();
    }
}

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment