Jxl. Writablecellformat Definition

Is it possible to create one jxl.WritableCellFormat attribute which contains Font, NumberFormat, BackgroundColor and Border?

This works:

public static final NumberFormat numberformatter = new NumberFormat("#,###0.00");  
public static final WritableFont defaultfont = new WritableFont(WritableFont.TAHOMA, 10); 
public static final WritableCellFormat numberCellFormat = new WritableCellFormat(defaultfont, numberformatter); '

but I can't get borders and colors, thought same kind of cell is needed many times when creating sheets.

1

2 Answers

You can set the border and background via the WritableCellFormat.setBorder() and WritableCellFormat.setBackground() methods after having instanciated your cell.

You can see the API there.

If you have to do that a lot, you can make a helper function like this :

public static makeCell(NumberFormat format, WritableFont font, Color backgrd, Border border){
    final WritableCellFormat result = new WritableCellFormat(defaultfont, numberformatter);
    result.setBorder(border);
    result.setBackground(backgrd);
    return result;
}
4

You Can do this by 2 steps

  1. Create a WritableFont

  2. Create a WritableCellFormat with WritableFont,NumberFormats as a parameter

Attaching the code snippet

int fontSize = 8;  
WritableFont wfontStatus = new WritableFont(WritableFont.ARIAL, fontSize);
wfontStatus.setColour(Colour.BLACK);
wfontStatus.setBoldStyle(WritableFont.BOLD);

WritableCellFormat fCellstatus = new WritableCellFormat(wfontStatus, NumberFormats.TEXT); // You can use any NumberFormats I have used TEXT for my requirement
try {
    fCellstatus.setWrap(true);
    fCellstatus.setBorder(Border.ALL,BorderLineStyle.THIN);
    fCellstatus.setAlignment(Alignment.CENTRE);
    fCellstatus.setBackground(Colour.YELLOW);
} 
catch (WriteException e) {
    e.printStackTrace();
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest