JExcelApi: multiple formats in one cell?

2019-07-21 02:00发布

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?

2条回答
Evening l夕情丶
2楼-- · 2019-07-21 02:37

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.

查看更多
虎瘦雄心在
3楼-- · 2019-07-21 02:47

@Cosmic There is another way to read that question: multiple formats in separate areas of a single cell.

Like: "Italics Bold Text" with "italics" and "bold" set in different style, i.e. bold not italics, respectively.

Can this be done in JExcelAPI? I am not aware of this. Anyone?

查看更多
登录 后发表回答