There are a few aspects of cells that you can configure: data, formulas, format, etc. They’re listed and described below.
DataA cell may contain a variety of types, like string, number, and date. To set a cell value programmatically, the reference needs to be retrieved from the getCell()
method. If a cell with the given coordinates exists, use the setCellValue()
method to update the cell value. Otherwise, it can be created from the createCell()
method, which takes the row and column coordinates and the cell value.
Note
Don’t forget to refresh the cells.
If cells that are changed in the active sheet that is currently being displayed in the browser, you need to callrefreshCells()
passing the cells changed to get them updated on the client side, as well. Formulas
You can use Excel formulas in the cells of the spreadsheet. The formulas are evaluated on the server, and the result is displayed in the cell.
FormatFormatting cell values can be accomplished by using cell styles. A cell style must be created in the workbook by using createCellStyle(). Cell data format is set for the style with setDataFormat().
Open in apackage com.vaadin.demo.component.spreadsheet; import java.util.GregorianCalendar; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormat; import com.vaadin.flow.router.Route; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.spreadsheet.Spreadsheet; @Route("spreadsheet-format") public class SpreadsheetFormat extends Div { public SpreadsheetFormat() { Spreadsheet spreadsheet = new Spreadsheet(); spreadsheet.setHeight("400px"); // tag::snippet[] // Define a cell style for dates CellStyle dateStyle = spreadsheet.getWorkbook().createCellStyle(); DataFormat format = spreadsheet.getWorkbook().createDataFormat(); dateStyle.setDataFormat(format.getFormat("yyyy-mm-dd")); // Add some data rows spreadsheet.createCell(1, 0, "Nicolaus"); spreadsheet.createCell(1, 1, "Copernicus"); spreadsheet.createCell(1, 2, new GregorianCalendar(1999, 2, 19).getTime()); // Style the date cell spreadsheet.getCell(1, 2).setCellStyle(dateStyle); // end::snippet[] add(spreadsheet); } }
Conditional FormattingApplying conditional formatting to cells can help in highlighting patterns in the data. The formatting is applied to the cells that match the specified condition. The condition can be based on the value of the cell, or on the value of another cell.
FontsCells can be styled by different fonts. A font definition not only includes a particular typeface, but also weight (e.g., bold or normal), emphasis, underlining, and other such font attributes.
A font definition is managed by Font class in the Apache POI API. A new font can be created with createFont() in the workbook.
Open in apackage com.vaadin.demo.component.spreadsheet; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import com.vaadin.flow.router.Route; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.spreadsheet.Spreadsheet; @Route("spreadsheet-fonts") public class SpreadsheetFonts extends Div { public SpreadsheetFonts() { Spreadsheet spreadsheet = new Spreadsheet(); spreadsheet.setHeight("400px"); // tag::snippet[] // Create a cell Cell cell = spreadsheet.createCell(2, 2, "Styled"); // Create and configure a cell style CellStyle cellStyle = spreadsheet.getWorkbook().createCellStyle(); // Add a blue bottom border cellStyle.setBorderBottom(BorderStyle.THICK); cellStyle.setBottomBorderColor(IndexedColors.BLUE.getIndex()); // Set the text to bold Font font = spreadsheet.getWorkbook().createFont(); font.setBold(true); cellStyle.setFont(font); // Apply the cell style to the cell cell.setCellStyle(cellStyle); // Request spreadsheet to refresh the cell spreadsheet.refreshCells(cell); // end::snippet[] add(spreadsheet); } }
Cells may include comments, which are shown when the mouse is hovered over the cells. They’re indicated by ticks in the corner of the cells. The SpreadsheetDefaultActionHandler enables adding comments from the context menu.
A new comment can be added through the POI API of a cell, with addComment(). For a detailed example on managing cell comments, refer to the InsertDeleteCellCommentAction and EditCellCommentAction classes employed by the default action handler.
Open in apackage com.vaadin.demo.component.spreadsheet; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import com.vaadin.flow.router.Route; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.spreadsheet.Spreadsheet; @Route("spreadsheet-comments") public class SpreadsheetComments extends Div { public SpreadsheetComments() { Spreadsheet spreadsheet = new Spreadsheet(); // tag::snippet[] Drawing<?> drawing = spreadsheet.getActiveSheet() .createDrawingPatriarch(); CreationHelper factory = spreadsheet.getActiveSheet().getWorkbook() .getCreationHelper(); ClientAnchor anchor = factory.createClientAnchor(); Comment comment = drawing.createCellComment(anchor); comment.setString(new XSSFRichTextString("First cell comment")); spreadsheet.createCell(0, 0, "cell").setCellComment(comment); // end::snippet[] spreadsheet.setHeight("400px"); add(spreadsheet); } }
Merging CellsYou can merge spreadsheet cells with any variant of the addMergedRegion() method in Spreadsheet. The SpreadsheetDefaultActionHandler enables merging selected cells from the context menu.
Merged cells can be unmerged with removeMergedRegion(). This method takes a region index as its parameter. You can search for a particular region through the POI Sheet API for the active sheet, which you can obtain with getActiveSheet().
The getMergedRegion() returns a merged region by index. You can iterate through them by knowing the number of regions, which you can find with getNumMergedRegions().
Components in CellsYou can have Vaadin components in spreadsheet cells and bind field components to the cell data. The components can be shown continuously, or function similar to editors that appear when a cell is activated for editing.
Components in a spreadsheet must be generated by a SpreadsheetComponentFactory, which you need to implement.
Open in apackage com.vaadin.demo.component.spreadsheet; import com.vaadin.flow.router.Route; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.Component; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.spreadsheet.Spreadsheet; import com.vaadin.flow.component.spreadsheet.SpreadsheetComponentFactory; import com.vaadin.flow.component.textfield.TextField; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; @Route("spreadsheet-components") public class SpreadsheetComponents extends Div { private Button customComponent; private TextField customEditor; public SpreadsheetComponents() { Spreadsheet spreadsheet = new Spreadsheet(); spreadsheet.setColumnWidth(1, 120); spreadsheet.createCell(1, 1, "Button"); spreadsheet.setColumnWidth(2, 120); spreadsheet.createCell(1, 2, "Text Field"); // tag::snippet[] spreadsheet.setSpreadsheetComponentFactory( new SpreadsheetComponentFactory() { @Override public Component getCustomComponentForCell(Cell cell, int rowIndex, int columnIndex, Spreadsheet spreadsheet, Sheet sheet) { if (spreadsheet.getActiveSheetIndex() == 0 && rowIndex == 2 && columnIndex == 1) { if (customComponent == null) { initCustomComponent(); } return customComponent; } return null; } @Override public Component getCustomEditorForCell(Cell cell, int rowIndex, int columnIndex, Spreadsheet spreadsheet, Sheet sheet) { if (spreadsheet.getActiveSheetIndex() == 0 && rowIndex == 2 && columnIndex == 2) { if (customEditor == null) { initCustomEditor(rowIndex, columnIndex, spreadsheet); } return customEditor; } return null; } @Override public void onCustomEditorDisplayed(Cell cell, int rowIndex, int columnIndex, Spreadsheet spreadsheet, Sheet sheet, Component editor) { if (cell == null) { return; } ((TextField) editor) .setValue(cell.getStringCellValue()); } }); // end::snippet[] spreadsheet.setHeight("400px"); add(spreadsheet); } private void initCustomComponent() { customComponent = new Button("Click"); } private void initCustomEditor(int rowIndex, int columnIndex, Spreadsheet spreadsheet) { customEditor = new TextField(); customEditor.addValueChangeListener(e -> spreadsheet.refreshCells( spreadsheet.createCell(rowIndex, columnIndex, e.getValue()))); } }
HyperlinksHyperlinks in cells can point to other worksheets in the current workbook — or to external URLs. Links must be added through the POI API.
Vaadin Spreadsheet provides default handling for hyperlink clicks. These can be overridden with a custom HyperlinkCellClickHandler, which you assign with setHyperlinkCellClickHandler().
Pop-Up ButtonsYou can add a pop-up button in a cell, which will open a drop-down, pop-up overlay when clicked. The overlay can contain any Vaadin components. You can add a pop-up button with any of the setPopup() methods for different cell addressing forms.
You can create pop-up buttons for a row of cells in a cell range by defining a table, as described in "Tables & Filters".
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4