package test import "github.com/plandem/xlsx"
//学生结构体 type Stu struct { Name string Age string } //读取表中数据,返回list func RedExcel()([]Stu) { filePath :="./test.xlsx" list := make([]Stu,0) //定义表头映射的字段数组 head:=[]string{"name","age"} xl, err := xlsx.Open(filePath) if err != nil { fmt.Print("err") return list } defer xl.Close() sheet := xl.Sheet(0, xlsx.SheetModeStream) totalCols, totalRows := sheet.Dimension() for row := 1; row < totalRows; row++ { stu := Stu{ Name: "", Age: "", } for col := 0; col < totalCols; col++ { value := sheet.Cell(col, row).String() field :=head[col] switch field { case "name": stu.Name=value case "age": stu.Age=value } } list=append(list,stu) } return list }