V4 V5

A Workbook holds a collection of worksheets plus anything that spans the whole book, such as defined names. It has no dependency on a control, so it works just as well from code that never shows a window.

The namespace is unvell.ReoGrid.Core.

Creating a book and adding sheets

using unvell.ReoGrid.Core;

var wb = new Workbook();
var sheet = wb.AddWorksheet("Sheet1");

// Omit rows/cols for Excel's full dimensions. Sparse storage, so this costs nothing.
var small = wb.AddWorksheet("Log", rows: 5000, cols: 32);

new Workbook() starts with no sheets at all, so add at least one. (A control does start with a sheet — new ReoGridControl() gives you Sheet1.)

Pass a smaller rows / cols only when you want to cap that sheet’s logical size. There is no performance reason to make a sheet small: storage costs are paid per cell that actually exists, so a hundred full-size sheets cost close to nothing while they are empty.

Finding a sheet

Worksheet first = wb[0];                    // by position
Worksheet? byName = wb["Sheet1"];           // by name (null when not found)
Worksheet? resolved = wb.ResolveSheet("Sheet1");

int count = wb.Count;
IReadOnlyList<Worksheet> all = wb.Worksheets;

Lookup by name is case-insensitive. wb["Name"] and ResolveSheet(...) are the same thing, and both return null when there is no such sheet.

The active sheet, and removing sheets

wb.ActiveSheetIndex = 1;
Worksheet? active = wb.ActiveWorksheet;

wb.Remove(ws);      // remove. Formula references heal to #REF!

ActiveSheetIndex is state on the model and is persisted with the file. The sheet a control is currently showing is control.ActiveWorksheet; calling control.SetWorksheet(ws) moves both.

Removing or renaming a sheet updates references from other sheets automatically — both cross-sheet references like Sheet2!A1 and defined names. A formula pointing at something that was deleted becomes #REF!.

Size limits

int maxRows = Workbook.MaxRows;       // 1,048,576
int maxCols = Workbook.MaxColumns;    //     16,384

The same limits as Excel, and the defaults for AddWorksheet.

Members

MemberWhat it does
AddWorksheet(name, rows, cols)Adds a sheet and returns it
Worksheets / CountThe sheets, and how many there are
this[int] / this[string]Get by position or name (name returns null when missing)
ResolveSheet(name)Same as this[string]
Remove(ws)Removes a sheet
ActiveSheetIndex / ActiveWorksheetThe active sheet
NamesThe defined-name registry (Defined Names)
DefineName / GetName / RemoveName / GetNamesWorking with defined names
RecalcCrossSheetFormulas()Recalculates formulas that reference another sheet
MaxRows / MaxColumnsThe size limits (constants)
Was this article helpful?