### Full XLS to CSV Export Workflow (Go) Source: https://context7.com/extrame/xls/llms.txt Provides a complete example of reading an XLS file, iterating through its sheets and rows, and exporting the data to separate CSV files. It includes error handling for file opening and skips hidden sheets, demonstrating a robust data extraction process. ```Go package main import ( "encoding/csv" "fmt" "os" "github.com/extrame/xls" ) func main() { // Open the XLS file xlFile, err := xls.Open("quarterly_report.xls", "utf-8") if err != nil { fmt.Printf("Failed to open file: %v\n", err) os.Exit(1) } fmt.Printf("Workbook Author: %s\n", xlFile.Author) fmt.Printf("Total Sheets: %d\n\n", xlFile.NumSheets()) // Process each sheet for sheetIdx := 0; sheetIdx < xlFile.NumSheets(); sheetIdx++ { sheet := xlFile.GetSheet(sheetIdx) if sheet == nil { continue } // Skip hidden sheets if sheet.Visibility != xls.WorkSheetVisible { fmt.Printf("Skipping hidden sheet: %s\n", sheet.Name) continue } fmt.Printf("Processing sheet: %s (%d rows)\n", sheet.Name, sheet.MaxRow+1) // Create CSV output file for this sheet csvFile, _ := os.Create(fmt.Sprintf("%s.csv", sheet.Name)) writer := csv.NewWriter(csvFile) // Extract all rows for rowIdx := 0; rowIdx <= int(sheet.MaxRow); rowIdx++ { row := sheet.Row(rowIdx) if row == nil { continue } // Build row data rowData := make([]string, row.LastCol()+1) for colIdx := row.FirstCol(); colIdx <= row.LastCol(); colIdx++ { rowData[colIdx] = row.Col(colIdx) } writer.Write(rowData) } writer.Flush() csvFile.Close() fmt.Printf(" Exported to %s.csv\n", sheet.Name) } } ``` -------------------------------- ### Get Populated Column Range in Row (Go) Source: https://context7.com/extrame/xls/llms.txt Illustrates how to use `FirstCol` and `LastCol` methods to determine the range of columns that contain data within a specific row. This is efficient for processing rows with sparse data, as it avoids iterating over empty columns. ```Go package main import ( "fmt" "github.com/extrame/xls" ) func main() { xlFile, _ := xls.Open("sparse_data.xls", "utf-8") sheet := xlFile.GetSheet(0) for i := 0; i <= int(sheet.MaxRow); i++ { row := sheet.Row(i) if row != nil { first := row.FirstCol() last := row.LastCol() fmt.Printf("Row %d: data from column %d to %d\n", i, first, last) // Read only populated columns for col := first; col <= last; col++ { fmt.Printf(" [%d]: %s\n", col, row.Col(col)) } } } } ``` -------------------------------- ### Get First Cell Value from Merged Cells (Go) Source: https://context7.com/extrame/xls/llms.txt Demonstrates using `ColExact` to retrieve the string value of a cell. This method differs from `Col` by returning an empty string for non-first cells within a merged cell range, effectively providing the value only for the primary cell of the merge. ```Go package main import ( "fmt" "github.com/extrame/xls" ) func main() { xlFile, _ := xls.Open("merged_cells.xls", "utf-8") sheet := xlFile.GetSheet(0) for i := 0; i <= int(sheet.MaxRow); i++ { row := sheet.Row(i) if row != nil { for col := row.FirstCol(); col <= row.LastCol(); col++ { // ColExact returns empty string for non-first cells in merged range value := row.ColExact(col) if value != "" { fmt.Printf("[%d,%d] = %s\n", i, col, value) } } } } } ``` -------------------------------- ### Open XLS Files Source: https://context7.com/extrame/xls/llms.txt Demonstrates different methods to open an Excel file: using a file path, using a closer for resource management, and using an io.ReadSeeker for memory or stream-based access. ```go xlFile, err := xls.Open("data/sales_report.xls", "utf-8") xlFile, closer, err := xls.OpenWithCloser("data/inventory.xls", "utf-8") defer closer.Close() reader := bytes.NewReader(data) xlFile, err := xls.OpenReader(reader, "utf-8") ``` -------------------------------- ### Iterate Rows in XLS File (Go) Source: https://context7.com/extrame/xls/llms.txt Demonstrates how to open an XLS file and iterate through all rows, printing the first and last column index for each row that contains data. This is useful for understanding the structure of the data within each row. ```Go package main import ( "fmt" "github.com/extrame/xls" ) func main() { xlFile, _ := xls.Open("data.xls", "utf-8") sheet := xlFile.GetSheet(0) // Iterate through all rows for i := 0; i <= int(sheet.MaxRow); i++ { row := sheet.Row(i) if row != nil { fmt.Printf("Row %d: columns %d to %d\n", i, row.FirstCol(), row.LastCol()) } } } ``` -------------------------------- ### Access Workbook Sheets Source: https://context7.com/extrame/xls/llms.txt Shows how to retrieve the number of sheets in a workbook and access specific sheets by their zero-based index. ```go sheetCount := xlFile.NumSheets() sheet := xlFile.GetSheet(0) ``` -------------------------------- ### Read Workbook Data Source: https://context7.com/extrame/xls/llms.txt Demonstrates how to extract cell data from a workbook, including reading all cells into a 2D slice for batch processing. ```go allCells := xlFile.ReadAllCells(1000) for rowIdx, row := range allCells { for colIdx, cell := range row { fmt.Printf("[%d]=%s ", colIdx, cell) } } ``` -------------------------------- ### Read Cell Values from XLS Row (Go) Source: https://context7.com/extrame/xls/llms.txt Shows how to read the string value of a cell at a specific column index within a row. It handles both header and data rows, extracting values for columns like name, amount, and date. For merged cells, `Col` returns the value for all cells in the merged range. ```Go package main import ( "fmt" "github.com/extrame/xls" ) func main() { xlFile, _ := xls.Open("sales.xls", "utf-8") sheet := xlFile.GetSheet(0) // Read header row headerRow := sheet.Row(0) if headerRow != nil { for col := headerRow.FirstCol(); col <= headerRow.LastCol(); col++ { value := headerRow.Col(col) fmt.Printf("Column %d: %s\n", col, value) } } // Read data rows for i := 1; i <= int(sheet.MaxRow); i++ { row := sheet.Row(i) if row != nil { name := row.Col(0) // First column amount := row.Col(1) // Second column date := row.Col(2) // Third column fmt.Printf("%s | %s | %s\n", name, amount, date) } } } ``` -------------------------------- ### Row Data Access Source: https://context7.com/extrame/xls/llms.txt Methods for retrieving rows and identifying column boundaries within a sheet. ```APIDOC ## GET /sheet/row ### Description Retrieves a specific row object by its zero-based index from a sheet. Returns nil if the row is empty or does not exist. ### Method GET ### Endpoint Row(index int) ### Parameters #### Path Parameters - **index** (int) - Required - The zero-based index of the row to retrieve. ### Response #### Success Response (200) - **row** (object) - The row object containing column data. ## GET /row/first-last-col ### Description Retrieves the first and last column indices that contain data within a specific row. ### Method GET ### Endpoint Row.FirstCol() / Row.LastCol() ### Response #### Success Response (200) - **index** (int) - The column index. ``` -------------------------------- ### Cell Data Extraction Source: https://context7.com/extrame/xls/llms.txt Methods for retrieving string values from specific cells within a row. ```APIDOC ## GET /row/col ### Description Gets the string value of a cell at the specified column index. For merged cells, it returns the value for all cells in the merged range. ### Method GET ### Endpoint Row.Col(colIndex int) ### Parameters #### Path Parameters - **colIndex** (int) - Required - The column index to retrieve. ### Response #### Success Response (200) - **value** (string) - The content of the cell. ## GET /row/col-exact ### Description Gets the string value of a cell at the specified column index. Unlike Col, for merged cells, it only returns the value for the first cell in the range. ### Method GET ### Endpoint Row.ColExact(colIndex int) ### Parameters #### Path Parameters - **colIndex** (int) - Required - The column index to retrieve. ### Response #### Success Response (200) - **value** (string) - The content of the cell (empty string for non-first cells in a merged range). ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.