问题
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?
回答1:
@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?
回答2:
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.
来源:https://stackoverflow.com/questions/1664862/jexcelapi-multiple-formats-in-one-cell