every call to "workbook.createFont()" adds a new font to the workbook. Is there a convenient way to reuse these fonts?
Obviously, I can reuse a font object within the program. But when I reopen the Java program, I need a way to get the Font object back.
While it's possible to use "workbook.getNumberOfFonts()" and "workbook.getFontAt(i)", using these methods is not really convenient.
My Workaround:
I add a CustomProperty when creating the font, which holds the corresponding number of the font.
Map<String, Font> fonts = new HashMap<>();
CustomProperties customProps = workbook.getProperties().getCustomProperties();
if(customProps.contains("arial_12_b")) {
short index = customProps.getProperty("arial_12_b").getI2();
fonts.put("arial_12_b", workbook.getFontAt(index));
} else {
Font font = workbook.createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short)12);
font.setBold(true);
customProps.addProperty("arial_12_b", font.getIndex());
fonts.put("arial_12_b", font);
}
CellStyle solved the same problem by the CellUtil class, which checks if the CellStyle already exists and creates it if it does not. Is there something simular for Font?
Best regards
AFoeee
One could providing a method which is using Workbook.findFont to achieve what you are asking about. To be much like CellUtil.setCellStyleProperties it could using a Map<FontProperty, Object> fontproperties
to set the font properties where FontProperty
is a enumeration of all the properties Workbook.findFont
currently checks. This are BOLD, COLOR, FONTHEIGHT, FONTNAME, ITALIC, STRIKEOUT, TYPEOFFSET, UNDERLINE for now.
Example:
//method for getting current font from cell
private static Font getFont(Cell cell) {
Workbook wb = cell.getRow().getSheet().getWorkbook();
CellStyle style = cell.getCellStyle();
return wb.getFontAt(style.getFontIndex());
}
private enum FontProperty {
BOLD, COLOR, FONTHEIGHT, FONTNAME, ITALIC, STRIKEOUT, TYPEOFFSET, UNDERLINE
}
//method for getting font having special settings additional to given source font
private static Font getFont(Workbook wb, Font fontSrc, Map<FontProperty, Object> fontproperties) {
boolean isBold = fontSrc.getBold();
short color = fontSrc.getColor();
short fontHeight = fontSrc.getFontHeight();
String fontName = fontSrc.getFontName();
boolean isItalic = fontSrc.getItalic();
boolean isStrikeout = fontSrc.getStrikeout();
short typeOffset = fontSrc.getTypeOffset();
byte underline = fontSrc.getUnderline();
for (FontProperty property : fontproperties.keySet()) {
switch (property) {
case BOLD:
isBold = (boolean)fontproperties.get(property);
break;
case COLOR:
color = (short)fontproperties.get(property);
break;
case FONTHEIGHT:
fontHeight = (short)fontproperties.get(property);
break;
case FONTNAME:
fontName = (String)fontproperties.get(property);
break;
case ITALIC:
isItalic = (boolean)fontproperties.get(property);
break;
case STRIKEOUT:
isStrikeout = (boolean)fontproperties.get(property);
break;
case TYPEOFFSET:
typeOffset = (short)fontproperties.get(property);
break;
case UNDERLINE:
underline = (byte)fontproperties.get(property);
break;
}
}
Font font = wb.findFont(isBold, color, fontHeight, fontName, isItalic, isStrikeout, typeOffset, underline);
if (font == null) {
font = wb.createFont();
font.setBold(isBold);
font.setColor(color);
font.setFontHeight(fontHeight);
font.setFontName(fontName);
font.setItalic(isItalic);
font.setStrikeout(isStrikeout);
font.setTypeOffset(typeOffset);
font.setUnderline(underline);
}
return font;
}
In this code Workbook.findFont
is used for trying to find a font having all the properties already. Only if this is not found (if (font == null)
) then a new font will be created.
How to use:
//set new cell D6 having special font settings
row = CellUtil.getRow(5, sheet);
cell = CellUtil.getCell(row, 3);
fontproperties = new HashMap<FontProperty, Object>();
fontproperties.put(FontProperty.BOLD, true);
fontproperties.put(FontProperty.COLOR, IndexedColors.BLUE.getIndex());
fontproperties.put(FontProperty.FONTHEIGHT, (short)(20*20));
fontproperties.put(FontProperty.FONTNAME, "Courier New");
fontproperties.put(FontProperty.STRIKEOUT, true);
fontproperties.put(FontProperty.UNDERLINE, Font.U_DOUBLE);
font = getFont(wb, getFont(cell), fontproperties);
styleproperties = new HashMap<String, Object>();
styleproperties.put(CellUtil.FONT, font.getIndex());
CellUtil.setCellStyleProperties(cell, styleproperties);
cell.setCellValue("new cell");
See How to format Excel Cell as Date in Apache POI for a complete example.