Vaadin spreadsheet cell styling

Hello, I’m still implementing and testing the vaadin spreadsheet and I noticed that when integrated with CUBA, the spreadsheet formatting doesn’t hold.
For example, when following the vaading example:
sheet.createCell(0, 0, “First Name”);
sheet.createCell(0, 1, “Last Name”);
sheet.createCell(0, 2, “Born”);

// Create a bold font
Font bold = sheet.getWorkbook().createFont();
bold.setBold(true);

// Set the cells in the first row as bold
for (int col=0; col <= 2; col++)
sheet.getCell(0, col).getCellStyle().setFont(bold);

In my spreadsheet, the first row is not set to bold. I’ve also tried with other colors but it doesn’t work.
Could it be that i have to somehow edit the style in my Halo.csss and reference them in my code?
Thanks.

Hello,
according to Vaadin Spreadsheet Tutorial you should clone font and style from workbook (Using HSSF API), then set its parameters and set font for style and style for cell. Take a look at: Vaadin Docs

You can use setCellsBold method from this example for any existing cells:


public class SpreadsheetDemo extends AbstractWindow {

    @Inject
    private VBoxLayout spreadsheetContainer;

    private Spreadsheet sheet;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);

        sheet = new Spreadsheet();

        sheet.createCell(0, 0, "First Name");
        sheet.createCell(0, 1, "Last Name");
        sheet.createCell(0, 2, "Born");

        Font bold = sheet.getWorkbook().createFont();
        bold.setBold(true);

        List<Cell> cellsToRefresh = new ArrayList<>();
        for (int col = 0; col <= 2; col++) {
            Cell cell = sheet.getCell(0, col);
            cell.getCellStyle().setFont(bold);

            cellsToRefresh.add(cell);
        }

        setCellsBold(sheet, cellsToRefresh);

        ComponentContainer vContainer = (ComponentContainer) WebComponentsHelper.unwrap(spreadsheetContainer);
        vContainer.addComponent(sheet);
    }

    private void setCellsBold(Spreadsheet sheet, List<Cell> cellsToRefresh) {
        for (Cell cell : cellsToRefresh) {
            // Clone Cell CellStyle
            CellStyle style = cloneStyle(sheet, cell);

            // Clone CellStyle Font
            Font font = cloneFont(sheet, style);

            font.setBold(true);

            style.setFont(font);

            cell.setCellStyle(style);
        }

        // Update all edited cells
        sheet.refreshCells(cellsToRefresh);
    }

    private CellStyle cloneStyle(Spreadsheet sheet, Cell cell) {
        CellStyle newStyle = sheet.getWorkbook().createCellStyle();
        newStyle.cloneStyleFrom(cell.getCellStyle());
        return newStyle;
    }

    private Font cloneFont(Spreadsheet sheet, CellStyle cellstyle) {
        Font newFont = sheet.getWorkbook().createFont();
        Font originalFont = sheet.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 can only be done when using .xlsx files

            XSSFFont originalXFont = (XSSFFont) originalFont;

            XSSFFont newXFont = (XSSFFont) newFont;

            newXFont.setColor(originalXFont.getXSSFColor());
        }

        return newFont;
    }
}

it works like a charm. Thanks.