读excel操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import ( "fmt" "github.com/tealeg/xlsx" ) func main() { excelFileName := "这里是excel的路径" xlFile, error := xlsx.OpenFile(excelFileName) if error != nil { ... } for _, sheet := range xlFile.Sheets { for _, row := range sheet.Rows { for _, cell := range row.Cells { fmt.Printf("%s\n", cell.String()) } } } } |
写入excel:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package main import ( "os" "encoding/csv") func main() { f, err := os.Create("test.xls") if err != nil { panic(err) } defer f.Close() f.WriteString("\xEF\xBB\xBF") // 写入UTF-8 BOM w := csv.NewWriter(f) w.Write([]string{"编号","姓名","年龄"}) w.Write([]string{"1","张三","23"}) w.Write([]string{"2","李四","24"}) w.Write([]string{"3","王五","25"}) w.Write([]string{"4","赵六","26"}) w.Flush() } |
golang 实现对excel的操作
有疑问加站长微信联系(非本文作者)