保存到文件,然后用全地址下载
package main
import (
"github.com/360EntSecGroup-Skylar/excelize/v2"
"log"
"strconv"
)
type test struct {
Id int `json:"id"`
Name string `json:"name"`
}
func main() {
titleList := []string{"ID", "用户"}
data := make([]test, 0)
for i := 0; i < 10; i++ {
m := test{
Id: i,
Name: "zxc",
}
data = append(data, m)
}
f := excelize.NewFile()
if err := f.SetSheetRow("Sheet1", "A1", &titleList); err != nil {
log.Fatal(err)
}
//表头被第一行用了,只能从第二行开始
j := 2
for _, vv := range data {
if err := f.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{vv.Id, vv.Name}); err != nil {
log.Fatal(err)
}
j++
}
if err := f.SaveAs("zxc.xlsx"); err != nil {
log.Fatal(err)
}
log.Println("OK")
}