Working with Spreadsheet Management

Working with Spreadsheet Management

Working with Spreadsheet Management

Spreadsheet management covers the day-to-day object surface of the crate: worksheets, cell collections, typed values, formulas, styling, and the structural features that make a workbook usable. The entry point is always a Workbook.


Worksheets

Sheets are managed through the workbook’s worksheet collections — read access via get_worksheets, mutation via get_worksheets_mut. Sheets are addressed by index (get(0)) or name (get_by_name), renamed with set_name, and added with add_worksheet:

let mut workbook = Workbook::new();
{
    let mut worksheets = workbook.get_worksheets_mut();
    let sheet = worksheets.get(0)?;
    sheet.set_name("Data")?;
}

Cell Access and Typed Values

Cells are addressed A1-style or numerically. get_by_index(row, column) suits generated layouts:

let mut cells = sheet.get_cells_mut();
cells.get("A1")?.put_value_string("Month")?;
for month in 1..=12_u32 {
    cells
        .get_by_index(month, 0)
        .put_value_string(format!("Month {month}"))?;
}

Reading back, display_string_value returns the display text and value_type reports the stored type.


Formulas with Cached Results

cells.get("F1")?.put_value_i32(10)?;
cells.get("G1")?
    .put_formula_with_cached_value("=F1*2", CellValue::Number(20.0))?;

The cached value is what Excel displays before its first recalculation — supply it so consumers of the file see correct results immediately.


Styling

CellStyle aggregates Font, Fill, and Borders; StyleFlag controls which style aspects an update applies, so existing formatting is preserved where you do not override it. NumberFormat governs how values render.


Page Setup and Navigation

PageSetup manages print layout: PaperSizeType, PageOrientationType, and margins. FreezePane keeps header rows visible while scrolling, and row and column visibility uses VisibilityType.


Structure: Tables, Names, Comments, Hyperlinks

  • ListObject turns a range into a structured table with TableStyleType styling and TotalsCalculation totals rows.
  • DefinedName and DefinedNameCollection manage named ranges.
  • Comment / CommentCollection and Hyperlink / HyperlinkCollection cover annotation and navigation.

Tips and Best Practices

  • Keep mutable borrows (get_worksheets_mut, get_cells_mut) inside a scope block; save the workbook after the block closes.
  • Use get_by_index for loops and generated layouts; A1 names for fixed positions.
  • Apply styles with a StyleFlag so unrelated formatting is untouched.
  • Name important ranges with DefinedName instead of hard-coding addresses.

Common Issues

IssueCauseFix
Borrow error saving the workbookMutable worksheet borrow still aliveClose the scope block before calling save
Formula shows 0 in ExcelNo cached value suppliedUse put_formula_with_cached_value with the expected result
Sheet not foundName mismatchAddress by index or check set_name spelling

FAQ

How do I add a second worksheet?

Call add_worksheet on the workbook, then fetch it via the worksheet collection by index or name.

Can I read and write in the same pass?

Yes — load with Workbook::load_xlsx, mutate through get_worksheets_mut, and save to the same or a new path.

Do typed setters convert values?

No conversions are applied: each put_value_* setter stores its native type; display_string_value renders the display text on read.


API Reference Summary

Class/MethodDescription
Workbook.add_worksheetAdd a sheet to the workbook
Worksheet.set_nameRename a sheet
Worksheet.get_cells_mutMutable cell collection
CellMut.put_value_stringTyped string setter
CellMut.put_formula_with_cached_valueFormula plus cached result
CellStyleFont, fill, and border formatting
StyleFlagSelects which style aspects apply
PageSetupPrint and page layout
ListObjectStructured tables

See Also