Working with Spreadsheet Management
Working with Spreadsheet Management
This guide walks through the day-to-day operations of building and modifying spreadsheets: creating workbooks, writing cell values, adding formulas, building tables, and exporting to CSV.
Create a Workbook and Write Cells
NewWorkbook() returns a workbook with one worksheet already present. Write values with Cells().Set(ref, value):
wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]
ws.Cells().Set("A1", "Item")
ws.Cells().Set("B1", "Category")
ws.Cells().Set("C1", "Price")
ws.Cells().Set("A2", "Widget A")
ws.Cells().Set("B2", "Gadgets")
ws.Cells().Set("C2", 12.99)
wb.Save("outputfiles/inventory.xlsx")Read Cells Back
Cells().Get(ref) returns the *Cell for a reference along with an error if the reference is invalid:
wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]
ws.Cells().Set("C2", 12.99)
cell, err := ws.Cells().Get("C2")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("C2 = %v\n", cell.Value)Use Cells().All() to enumerate every populated cell in the collection when you need to iterate over the whole sheet.
Add a Formula
Cell.SetFormula stores a formula string on a cell. Create the cell first with Set, then attach the formula — the formula is evaluated by Excel or LibreOffice on open, not by the library:
wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]
ws.Cells().Set("D2", nil)
cell, _ := ws.Cells().Get("D2")
cell.SetFormula("C2*2")Build a Table Over the Data
Once your data range is populated, Worksheet.AddTable turns it into a structured, named table:
wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]
ws.Cells().Set("A1", "Item")
ws.Cells().Set("A2", "Widget A")
tbl := ws.AddTable("A1:C2")
tbl.HasHeaderRow = true
tbl.StyleName = "TableStyleMedium6"
fmt.Printf("Created table %q covering %s\n", tbl.Name, tbl.Range)Export to CSV
Workbook.ExportToCSV writes a given worksheet index to a CSV file with the delimiter of your choice:
wb := cells_foss.NewWorkbook()
if err := wb.ExportToCSV(0, "outputfiles/inventory.csv", ','); err != nil {
fmt.Fprintf(os.Stderr, "Error exporting: %v\n", err)
os.Exit(1)
}The reverse direction, Workbook.ImportFromCSV(path, sheetName, delimiter), reads a CSV file into a named worksheet.
Remove a Cell
Cells().Remove(ref) clears a cell’s value:
wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]
ws.Cells().Set("A2", "Widget A")
if err := ws.Cells().Remove("A2"); err != nil {
fmt.Printf("Error removing cell: %v\n", err)
}Tips and Best Practices
- Create a cell with
Setbefore callingSetFormulaon it —SetFormulaoperates on an existing*Cell, not a bare reference string. - When exporting a workbook that has multiple worksheets,
ExportToCSV’s sheet index parameter determines which worksheet is exported — index0is the first worksheet. - Use
Cells().All()for read-heavy iteration over a whole sheet rather than callingGetin a loop over every possible reference.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
SetFormula has no effect | Called on a cell reference that was never created with Set first | Call ws.Cells().Set(ref, nil) to create the cell, then Get it and call SetFormula |
| CSV export is empty or wrong sheet | Wrong worksheet index passed to ExportToCSV | Confirm the index matches the intended worksheet’s position in Workbook.Worksheets |
Get returns an error | The cell reference does not exist in the collection | Confirm the reference was previously set with Set, or handle the “not found” case explicitly |
FAQ
Does Cells().Remove delete the whole row or column?
No — it clears a single cell’s value at the given reference.
Can I build a table over a range that already has a formula column?
Yes — AddTable operates on the cell range as written; formulas in the range are preserved.
API Reference Summary
| Class / Method | Description |
|---|---|
Workbook.NewWorkbook | Create a new workbook with one worksheet |
Cells.Set / Get / Remove / All | Write, read, remove, and enumerate cells |
Cell.SetFormula / GetFormula | Attach and read a formula string |
Worksheet.AddTable | Build a structured table over a cell range |
Workbook.ExportToCSV / ImportFromCSV | Convert between XLSX and CSV |
See Also
- Feature Overview: Styling, data validation, tables, pictures, and streaming
- Aspose.Cells — Enterprise Product Family