开发者

JExcelApi: multiple formats in one cell?

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?


@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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜