Golang export excel

Export all document contents to Excel by clicking the Export button. Add the route first

r.HandleFunc("/cusexcel", web.Cusexcel)

Export using the web.Cusexcel method

func Cusexcel(w http.ResponseWriter, r *http.Request) {
cutomers, _ := dbdata.Cuslist()
f := excelize.NewFile()
index := f.NewSheet("Sheet1")
Data: = [] [] interface {} {{" serial number ", "name", "customs code", "address", "contact", "phone", "contact content"}}
for _, cutomer := range cutomers {
row := []interface{}{
cutomer.Id,
cutomer.Name,
cutomer.Taxno,
cutomer.Addr,
cutomer.Contact,
cutomer.Contel,
cutomer.Content,
}
data = append(data, row)
}
for i, obj := range data {
//-- Concatenates cell names based on rows and columns
name, err := excelize.JoinCellName("A", i+1)
if err ! = nil {
Println(fmt.Sprintf(" Failed to concatenate cell name, error :%s", err))
return
}
err = f.SetSheetRow("Sheet1", name, &obj)
if err ! = nil {
Println(fmt.Sprintf(" Failed to write data to line, error :%s", err))
return
}
}
f.SetActiveSheet(index)
Filename := "client.xlsx"
w.Header().Add("Content-Type", "application/vnd.ms-excel")
w.Header().Add("Content-Disposition", "attachment;  filename="+filename)
f.Write(w)
}

Firstly, all records are queried from the database and stored in slices, and then files are created. The data are organized in the form of two-dimensional slices, and then the corresponding relationship between cells and data is created by circulating data. Finally, header information is set and returned by downloading files. Export excel library github.com/xuri/excelize/v2 need to introduce a third party

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish