Iram Khan
Iram Khan

Reputation: 37

Error reading an excel file in golang

I was trying the below code but I get an error

"multiple-value cell.String() in single-value context"

Code

 package main
import (
"fmt"
"github.com/tealeg/xlsx"
 )
 func main() { 
     excelFileName := "test.xlsx"
     xlFile, err := xlsx.OpenFile(excelFileName)
     if err != nil {

     }
 for _, sheet := range xlFile.Sheets {
    for _, row := range sheet.Rows {
          for _, cell := range row.Cells {
            fmt.Printf("%s ", cell.String(), "123")
  }
            fmt.Printf("\n")
  }
  }
 }
}

Upvotes: 1

Views: 2153

Answers (2)

matt.s
matt.s

Reputation: 1746

You are trying to use a function that returns multiple values as a single argument (to fmt.Printf())

If you don't think you will get any errors, you can just assign it to _ and ignore it, but you really should be prepared to handle the error. This is why Go forces you to use variables and why it doesn't automatically discard other return values (like the error from cell.String())

val, err := cell.String()
if err != nil {
    // handle the error
}
fmt.Printf("%s ", val) // you had an extra arg of "123"?

Upvotes: 0

Apin
Apin

Reputation: 2668

change

for _, cell := range row.Cells {
    fmt.Printf("%s ", cell.String(), "123")
}

to

for _, cell := range row.Cells {
    val, _ := cell.String()
    fmt.Printf("%s ", val)
}

Upvotes: 3

Related Questions