private Font cloneFont(CellStyle cellstyle) { Font newFont = spreadsheet.getWorkbook().createFont(); Font originalFont = spreadsheet.getWorkbook().getFontAt(cellstyle.getFontIndex()); if (originalFont != null) { newFont.setBold(originalFont.getBold()); newFont.setItalic(originalFont.getItalic()); newFont.setFontHeight(originalFont.getFontHeight()); newFont.setUnderline(originalFont.getUnderline()); newFont.setStrikeout(originalFont.getStrikeout()); // This cast an only be done when using .xlsx files XSSFFont originalXFont = (XSSFFont) originalFont; XSSFFont newXFont = (XSSFFont) newFont; newXFont.setColor(originalXFont.getXSSFColor()); } return newFont; }
private void updateSelectedCellsBold() { if (spreadsheet != null) { List<Cell> cellsToRefresh = new ArrayList<Cell>(); for (CellReference cellRef : spreadsheet.getSelectedCellReferences()) { // Obtain Cell using CellReference Cell cell = getOrCreateCell(cellRef); // Clone Cell CellStyle CellStyle style = cloneStyle(cell); // Clone CellStyle Font Font font = cloneFont(style); // Toggle current bold state font.setBold(!font.getBold()); style.setFont(font); cell.setCellStyle(style); cellsToRefresh.add(cell); } // Update all edited cells spreadsheet.refreshCells(cellsToRefresh); } }