# A Million Rows and Live Excel Formulas, Inside Your Own App

My high school finishing exams included a test in Lotus 1-2-3. I've spent a good part of my career since then yelling at spreadsheets, and I still use them every day: the company P&L, grocery lists, student attendance. People love to hate them. The world runs on them anyway, because nothing else lets a person who isn't a programmer build a working model of their business in an afternoon.

<aside><strong>What is Codename One?</strong> Codename One is an open-source framework for building native iOS, Android, desktop, and web apps from a single Java or Kotlin codebase. Learn more at <a href="https://www.codenameone.com/">codenameone.com</a>.</aside>

Lotus is worth remembering for another reason. It didn't invent the spreadsheet. VisiCalc did. Lotus put charts next to the numbers, and mixing two good ideas made one of the first great software companies. Here are the components. Remix them.

This week Codename One gained two of them. `DataGrid` is a grid for data at enterprise scale, and `Workbook` turns it into a spreadsheet with real files and live formulas.

## DataGrid is Table for a million rows

`Table` stays. It lays out a real component in every cell, which is what you want for a form with twenty fields. `DataGrid` is a different animal, closer to SwingX's `JXTable` and `JXTreeTable` if you remember those. It paints its own cells, like our Gantt chart and scheduler, so there's no component per row and only the visible rows cost anything.

If you've built a back office system you know why that matters. A parts catalog, a ledger, a log of sensor readings or a CRM export isn't a hundred rows. It's a hundred thousand on a slow day.

```java
DataGrid grid = new DataGrid(model);   // any TableModel, a million rows is fine
grid.setFrozenColumns(1);
grid.getColumn(AMOUNT).setAggregate(GridAggregate.SUM);
grid.getColumn(ORDER).setAggregate(GridAggregate.COUNT);
grid.setGroupColumn(grid.getColumn(REGION));
```

The developer guide's example hands the grid a model whose `getRowCount()` returns 1,000,000 and scrolls past row 500,001. Selection is stored as ranges, so selecting all million rows is one range, not a million objects. Sorting and filtering reorder an array of row numbers and never copy your data.

![A DataGrid with a frozen column, progress bars and colored amounts](https://www.codenameone.com/developer-guide/img/generated/datagrid-renderers.png align="center")

Frozen columns and custom renderers.

![A DataGrid scrolled deep into a million rows](https://www.codenameone.com/developer-guide/img/generated/datagrid-million.png align="center")

A million rows.

![A DataGrid grouped by region with sums and averages](https://www.codenameone.com/developer-guide/img/generated/datagrid-grouped.png align="center")

Grouping with aggregates.

![A tree grid with expandable rows](https://www.codenameone.com/developer-guide/img/generated/datagrid-tree.png align="center")

The tree grid.

The rest is the feature list of a grid you'd normally license: resizable, movable and hidden columns, flex widths, a stable multi-key sort, filters, keyboard navigation, single and multiple row selection, and in-place editing with date, boolean and choice pickers. Every column takes its own renderer, formatter and comparator, and the whole thing is styled with CSS, with light and dark modes and a native iOS look out of the box.

The tree mode takes a `TreeTableModel` and loads children lazily, so a node costs only what it shows when it opens. A bill of materials ten levels deep is fine.

Swing developers get a bonus. Our Swing compatibility layer's `JTable` and `JXTreeTable` now run on the same row engine, so the [Swing code you bring to a phone](https://www.codenameone.com/blog/swing-and-javafx-on-phones/) sorts a big table as fast as the native grid does.

## Then it becomes a spreadsheet

`Workbook` reads and writes `.xlsx`, `.ods` and CSV. Each `Sheet` is a `TableModel`, so it drops straight into a `DataGrid`, with the number formats from the file turned into column formatters:

```java
Workbook book = Workbook.read(in, Workbook.Format.detect(fileName, header));
Sheet sheet = book.getSheet(0);
DataGrid grid = new DataGrid(sheet);
grid.setColumns(sheet.createColumns());
```

![An orders sheet with formulas in a DataGrid](https://www.codenameone.com/developer-guide/img/generated/datagrid-sheet-orders.png align="center")

An orders sheet, live.

![A loan calculator built from spreadsheet formulas](https://www.codenameone.com/developer-guide/img/generated/datagrid-sheet-loan.png align="center")

A loan model with PMT.

![A summary sheet referencing other sheets](https://www.codenameone.com/developer-guide/img/generated/datagrid-sheet-summary.png align="center")

Cross-sheet references.

Formulas are live. Change a cell and every dependent cell recalculates, including on other sheets, and the grid repaints without a line of your code:

```java
Workbook book = new Workbook();
Sheet sheet = book.addSheet("Orders");
sheet.addRow("Item", "Units", "Price", "Total");
sheet.addRow("Bolt", 40.0, 0.25);
sheet.addRow("Bracket", 12.0, 3.5);
sheet.setCellFormula("D2", "=B2*C2");
sheet.setCellFormula("D3", "=B3*C3");
sheet.setCellFormula("D4", "=SUM(D2:D3)");
Object total = sheet.getCellValue("D4");   // 52.0
sheet.setCellValue("B2", 100.0);            // D2 and D4 update
```

The engine implements 402 Excel functions across math, statistics, text, dates, lookup, financial, engineering, database and dynamic arrays. `XLOOKUP`, `FILTER`, `SORT`, `SEQUENCE`, `LET` and `PMT` are all there, with cross-sheet references, defined names and Excel's own nesting limit of 64.

![Diagram](https://mermaid.ink/img/Zmxvd2NoYXJ0IExSCiAgICBYW1hMU1gsIE9EUywgQ1NWPGJyLz5mcm9tIGFueXdoZXJlIGluIHRoZSBvcmddIC0tPiBXW1dvcmtib29rXQogICAgVyAtLT4gU1tTaGVldCA9IFRhYmxlTW9kZWxdCiAgICBTIC0tPiBHW0RhdGFHcmlkPGJyLz5zb3J0LCBmaWx0ZXIsIGdyb3VwXQogICAgUyAtLT4gRls0MDIgbGl2ZSBmdW5jdGlvbnNdCiAgICBHIC0tPiBFW1dvcmtib29rLmZyb21HcmlkPGJyLz5leHBvcnQgd2hhdCB0aGUgdXNlciBzZWVzXQogICAgRSAtLT4gWDJbWExTWCBiYWNrIHRvIEV4Y2VsXQ==?type=png&bgColor=ffffff align="center")

### Typing a formula feels like Excel

This is the part I'm proudest of, because it's what makes a spreadsheet usable by the people who actually live in them. The formula editor is a one line code editor. It colors each reference and outlines the same cells in the grid in the same color. It completes function names with a description, shows the argument list with the current argument in bold, inserts a cell's address when you tap it, and cycles `$A$1` and the other reference forms on F4.

![Formula completion listing ROUND, ROUNDDOWN and ROUNDUP](https://www.codenameone.com/developer-guide/img/generated/datagrid-sheet-formula-complete.png align="center")

Function completion.

![A formula argument hint with the current argument in bold](https://www.codenameone.com/developer-guide/img/generated/datagrid-sheet-formula-hint.png align="center")

Argument hints and colored references.

`FormulaBar` is the ready-made field above the sheet, if you want the Excel layout without building it.

When the user is done, `Workbook.fromGrid` saves exactly what the grid shows: the sort, the filter, the groups as outline levels and the frozen columns. They open it in Excel and it looks the way they left it.

## What you can build with this

Every company has a folder of spreadsheets that are secretly applications. A pricing model in sales, a shift roster in operations, an inventory count somebody reconciles every Friday. Today those files get emailed around, and the version on someone's laptop is the one that matters.

Pull them into your app instead. Import the sheet, keep the formulas the finance team wrote, and connect the cells to everything else Codename One does: your backend's database, a [multi-tenant server](https://www.codenameone.com/blog/multi-tenant-saas-backend-in-java/), push notifications when a number crosses a line, a PDF of the result, a chart, a map. The model stays a spreadsheet the business can read. Everything around it is the part a spreadsheet could never do.

That's the Lotus move. Two ideas everyone already has, put together, make something nobody had.

Tomorrow: [the backend that keeps each customer's data its own](https://www.codenameone.com/blog/multi-tenant-saas-backend-in-java/).
