golang中的excel上傳,讀取數據


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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM