简体   繁体   中英

Apache POI Trouble With Font

I am getting a strange error using Apache POI version 3.15 (currently the most recent stable non-beta build) and my internet searches do not show anyone else with this error. I am trying to simply bold a cell, for example:

Workbook wb = new HSSFWorkbook();
HSSFFont font = wb.createFont();
font.setBold(true);

Already I run into an issue where it claims the font the workbook is creating isn't a HSSFFont, but some normal font:

incompatible types: Font cannot be converted to HSSFFont

I've tried creating a normal font and applying it to the CellStyle, but that causes a nullPointerException when adding it to the cellStyle. I'm at a loss.

wb.createFont() returns a org.apache.poi.ss.usermodel.Font , but if you inspect the runtime class of the Font it returns, it is actually a HSSFFont . You can try casting to a HSSFFont or just accessing it as a Font :

final Workbook wb = new HSSFWorkbook();

final HSSFFont hssfFont = (HSSFFont)wb.createFont();
hssfFont.setBold(true);

final Font font = wb.createFont();
font.setBold(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM