How to Load a Spreadsheet in Go
Aspose.Cells FOSS for Go loads an existing .xlsx workbook into memory as a Workbook, giving you full access to its worksheets and cells through the standard library API. Install the module with go get before starting.
Step-by-Step Guide
Step 1: Install the Package
go get github.com/aspose-cells-foss/Aspose.Cells-FOSS-for-Go/v26Verify the module version in your go.mod after installing.
Step 2: Import Required Classes
import (
cells_foss "github.com/aspose-cells-foss/Aspose.Cells-FOSS-for-Go/v26/aspose/cells_foss"
)Step 3: Create and Populate a Workbook to Load
This example first creates a workbook to have a real .xlsx file to load — in your own code, substitute the path to an existing spreadsheet:
wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]
ws.Cells().Set("A1", "Product")
ws.Cells().Set("B1", "Price")
ws.Cells().Set("A2", "Widget A")
ws.Cells().Set("B2", 12.99)
wb.Save("outputfiles/products.xlsx")Step 4: Read Cell Values from the Loaded Worksheet
Access the first worksheet through Worksheets[0] and read individual cells with Cells().Get(ref):
wb := cells_foss.NewWorkbook()
wb.Worksheets[0].Cells().Set("B2", 12.99)
ws2 := wb.Worksheets[0]
cell, err := ws2.Cells().Get("B2")
if err != nil {
fmt.Printf("Error reading cell: %v\n", err)
return
}
fmt.Printf("Price: %v\n", cell.Value)Step 5: Enumerate All Populated Cells
Use Cells().All() when you need to iterate over every cell that has been set, rather than reading known references one at a time:
wb := cells_foss.NewWorkbook()
ws2 := wb.Worksheets[0]
ws2.Cells().Set("A1", "Product")
allCells := ws2.Cells().All()
for ref, c := range allCells {
fmt.Printf("%s = %v\n", ref, c.Value)
}Common Issues and Fixes
Get returns an error for a cell reference. The reference has not been set in the collection yet. Confirm the cell was written with Set, or check the returned error explicitly before using the cell.
Values look like the wrong type. Cell.Value reflects whatever type was passed to Set (string, int, float64, and so on) — inspect it with a type switch if you need to branch on the underlying Go type.
Loaded workbook has no data on the expected sheet. Confirm you are indexing the correct entry in Worksheets — a workbook can have more than one worksheet, and the target data may not be on Worksheets[0].
Frequently Asked Questions
Does loading a workbook require the original file to still exist on disk?
Yes — Workbook reads from the file path you provide. Keep the source file accessible for the duration of the load.
Can I modify a cell after loading and save the changes back?
Yes — Cells().Set works on a loaded workbook the same way it does on a newly created one; call Save again to write your changes.
Is there a way to avoid loading the entire file into memory?
Yes — use StreamingReader.ProcessRows for large files where you only need to read rows sequentially rather than load the whole workbook.