一、安装依赖
go get github.com/tealeg/xlsx
二、main.go
package main
import "fmt"
import "github.com/tealeg/xlsx"
type Student struct {
Name string
Sex string
}
func (student Student) show() {
fmt.Printf("Name:%s Sex:%s\r\n", student.Name, student.Sex)
}
func main() {
excelPath := "students.xlsx"
xlFile, err := xlsx.OpenFile(excelPath)
if err != nil {
fmt.Printf("Error opening Excel file: %s\n", err)
return
}
sheet := xlFile.Sheets[0]
var students []Student
i := 0
for _, row := range sheet.Rows {
name := row.Cells[0].String()
sex := row.Cells[1].String()
fmt.Printf("name:%s sex:%s \r\n", name, sex)
students = append(students, Student{Name: name, Sex: sex})
i++
}
for _, student := range students {
student.show()
}
}
输出: