views:

322

answers:

2

In Excel, I can have multiple text styles in a single cell. Is there a way to create a file like this using JExcelApi? I'm not seeing anything so far: setCellFormat is a method on WritableCell, and there doesn't seem to be any way to set a format for anything within a single cell.

Am I just missing it (quite possible!), or is this not implemented?

As a bonus: how hard would this be to implement? Is there any other Excel-export library which does implement this, from which I could borrow the code?

Thanks!

A: 

Maybe you can try SmartXLS for java,it is a java spreadsheet component which can write, read, calculate Excel compatible files without the need for Microsoft Excel.

liya
Wow, is every answer of yours here an ad for SmartXLS? :-)
Ken
And no, that doesn't really help. It's a completely different library (so I'd have to port and re-debug all my existing code), and it's proprietary (so I can't just borrow the one piece I need), and it's not even clear that it does what I want (since the docs aren't very good, and the class hierarchy is basically nonexistent, and it generally looks more complex than JExcelApi).
Ken
A: 

With variables WritableSheet ws, int col, int row

The following code will set your cell's font to bold.

WritableCell wc = ws.getWritableCell(col, row);
WritableCellFormat cf = wc.getCellFormat() != null ? new WritableCellFormat(wc.getCellFormat()) : new WritableCellFormat();
WritableFont wf = new WritableFont(cf.getFont());

try {
        wf.setBoldStyle(WritableFont.BOLD);
        // refer to http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/write/WritableFont.html for other text styles
        cf.setFont(wf);

        wc.setCellFormat(cf);

    } catch ...

A CellFormat/WritableCellFormat contains lots of different formatting options, such as the font, borders, background colour and wrap.

So, yes. You were just missing it :p

EDIT: As I didn't make it clear enough, for multiple styles you can call multiple methods on your WritableFont, e.g setBoldStyle(), setItalic(), setUnderlineStyle(), setStruckout(), setColour(), etc.

Cosmic Flame