Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through column cells in golang and excelize

Tags:

excel

go

excelize

I was wondered how to loop over column cells of excel sheet in golang, here is my excel file:

enter image description here

I have tried this piece of code for other reason

package main

import (
    "fmt"
    "github.com/xuri/excelize/v2"
)

func main() {

    f, err := excelize.OpenFile("pricematching.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }

    // Get all the rows in the sheet1 section.
    rows, err := f.GetCellValue("sheet1", "A2")

    fmt.Print(rows, "\t")

}


like image 592
نعمان منذر محمود الجميلي Avatar asked Oct 24 '25 18:10

نعمان منذر محمود الجميلي


1 Answers

No matter how's your excel file, this is the way to read each cell:

xlsxFile, error := excelize.OpenFile(filePath)
for _, sheetName := range xlsxFile.GetSheetMap() {
   for rowIndex, rowValues := range xlsxFile.GetRows(sheetName) {
      for columnIndex, columnValue := range rowValues {
         // do what ever you want here
      }
   }
}
like image 71
Hamed Rajabi Avatar answered Oct 27 '25 07:10

Hamed Rajabi