如何在 Apache POI 中重用字体?
How to reuse Fonts in Apache POI?
每次调用 "workbook.createFont()" 都会向工作簿添加一种新字体。有没有方便的方法来重复使用这些字体?
显然,我可以在程序中重复使用字体对象。但是当我重新打开 Java 程序时,我需要一种方法来取回 Font 对象。
虽然可以使用 "workbook.getNumberOfFonts()" 和 "workbook.getFontAt(i)",但使用这些方法并不是很方便。
我的解决方法:
我在创建字体的时候添加了一个CustomProperty,里面保存着字体对应的编号。
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 通过 CellUtil class 解决了同样的问题,它检查 CellStyle 是否已经存在,如果不存在则创建它。有没有类似字体的东西?
此致
阿福
可以提供一种使用 Workbook.findFont to achieve what you are asking about. To be much like CellUtil.setCellStyleProperties 的方法,它可以使用 Map<FontProperty, Object> fontproperties
来设置字体属性,其中 FontProperty
是所有属性的枚举 Workbook.findFont
目前检查。现在是粗体、颜色、FONTHEIGHT、FONTNAME、ITALIC、删除线、TYPEOFFSET、下划线。
示例:
//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;
}
在此代码中,Workbook.findFont
用于尝试查找已具有所有属性的字体。仅当未找到 (if (font == null)
) 时才会创建新字体。
使用方法:
//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");
有关完整示例,请参阅 How to format Excel Cell as Date in Apache POI。
每次调用 "workbook.createFont()" 都会向工作簿添加一种新字体。有没有方便的方法来重复使用这些字体?
显然,我可以在程序中重复使用字体对象。但是当我重新打开 Java 程序时,我需要一种方法来取回 Font 对象。
虽然可以使用 "workbook.getNumberOfFonts()" 和 "workbook.getFontAt(i)",但使用这些方法并不是很方便。
我的解决方法:
我在创建字体的时候添加了一个CustomProperty,里面保存着字体对应的编号。
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 通过 CellUtil class 解决了同样的问题,它检查 CellStyle 是否已经存在,如果不存在则创建它。有没有类似字体的东西?
此致
阿福
可以提供一种使用 Workbook.findFont to achieve what you are asking about. To be much like CellUtil.setCellStyleProperties 的方法,它可以使用 Map<FontProperty, Object> fontproperties
来设置字体属性,其中 FontProperty
是所有属性的枚举 Workbook.findFont
目前检查。现在是粗体、颜色、FONTHEIGHT、FONTNAME、ITALIC、删除线、TYPEOFFSET、下划线。
示例:
//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;
}
在此代码中,Workbook.findFont
用于尝试查找已具有所有属性的字体。仅当未找到 (if (font == null)
) 时才会创建新字体。
使用方法:
//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");
有关完整示例,请参阅 How to format Excel Cell as Date in Apache POI。