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
ListObjectturns a range into a structured table withTableStyleTypestyling andTotalsCalculationtotals rows.DefinedNameandDefinedNameCollectionmanage named ranges.Comment/CommentCollectionandHyperlink/HyperlinkCollectioncover 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_indexfor loops and generated layouts; A1 names for fixed positions. - Apply styles with a
StyleFlagso unrelated formatting is untouched. - Name important ranges with
DefinedNameinstead of hard-coding addresses.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| Borrow error saving the workbook | Mutable worksheet borrow still alive | Close the scope block before calling save |
| Formula shows 0 in Excel | No cached value supplied | Use put_formula_with_cached_value with the expected result |
| Sheet not found | Name mismatch | Address 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/Method | Description |
|---|---|
Workbook.add_worksheet | Add a sheet to the workbook |
Worksheet.set_name | Rename a sheet |
Worksheet.get_cells_mut | Mutable cell collection |
CellMut.put_value_string | Typed string setter |
CellMut.put_formula_with_cached_value | Formula plus cached result |
CellStyle | Font, fill, and border formatting |
StyleFlag | Selects which style aspects apply |
PageSetup | Print and page layout |
ListObject | Structured tables |