download test.xlsx and then open the test.xlsx. a runtime error occur
here is my example.
anyone knows how to fix it
import (
"fmt"
"log"
"net/http"
"github.com/tealeg/xlsx"
)
func main() {
oexcel := func(w http.ResponseWriter, req *http.Request) {
_, err := xlsx.OpenFile("/tmp/test.xlsx") // here open you local file path
if err != nil {
fmt.Println(err)
return
}
fmt.Println("no err")
}
dexcel := func(w http.ResponseWriter, req *http.Request) {
headers := []string{"groupName", "groupID", "groupPath", "groupOwner"}
filename := "test"
w.Header().Add("Content-Disposition", "attachment;filename=\""+filename+".xlsx\"")
w.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
b := xlsx.NewStreamFileBuilder(w)
cellType := xlsx.CellTypeString
ct := []*xlsx.CellType{cellType.Ptr(), cellType.Ptr(), cellType.Ptr(), cellType.Ptr()}
if err := b.AddSheet("Sheet1", headers, ct); err != nil {
fmt.Println("end")
return
}
sf, err := b.Build()
fmt.Println(sf, err)
if err != nil {
fmt.Println("end")
return
}
defer sf.Close()
for i := 0; i < 100; i++ {
sf.Write([]string{"GroupName", "GroupID", "PathName", "OwnerAccount"})
}
return
}
http.HandleFunc("/openExcel", oexcel)
http.HandleFunc("/downloadExcel", dexcel)
log.Fatal(http.ListenAndServe(":8080", nil))
}